Back to index

wims  3.65+svn20090927
MyGraphicsPDF.java
Go to the documentation of this file.
00001 package rene.zirkel.graphics;
00002 
00003 import java.io.*;
00004 
00005 import java.awt.*;
00006 import java.awt.image.*;
00007 
00008 import rene.gui.Global;
00009 import rene.zirkel.ZirkelFrame;
00010 import rene.zirkel.construction.Construction;
00011 import rene.zirkel.objects.*;
00012 
00013 class PdfFontMetrics extends FontMetrics
00014 {      int Size;
00015 
00016        public PdfFontMetrics (int size)
00017        {      super(new Font("Courier",Font.PLAIN,size)); // a dummy font.
00018               Size=size;
00019        }
00020 
00021        public int stringWidth (String s)
00022        {      return s.length()*Size; 
00023        }
00024        
00025        public int getHeight()
00026        {      return Size; 
00027        }
00028        
00029        public int getAscent()
00030        {      return Size*4/5; 
00031        }
00032        
00033 }
00034 
00035 public class MyGraphicsPDF extends MyGraphics
00036 {      PrintWriter Out;
00037        StringBuffer buf,xref,stream;
00038        int nxref=1;
00039        String prefix="";
00040        final int Normal=0,Thin=1,Thick=2;
00041        int Stroke=Normal;
00042        private PdfFontMetrics FM=new PdfFontMetrics(10);       
00043        boolean Bold; 
00044        double W,H;
00045        double LineWidth;
00046 
00047        public MyGraphicsPDF (PrintWriter out, double w, double h, double linewidth)
00048        {      Out=out;
00049               LineWidth=linewidth;
00050               W=w; H=h;
00051               buf=new StringBuffer();
00052               xref=new StringBuffer();
00053               appendxref(0,65535,false);
00054               appendln("%PDF-1.4");
00055               newObject();
00056               appendln("<< /Type /Catalog");
00057               appendln("   /Outlines 2 0 R");
00058               appendln("   /Pages 3 0 R");
00059               appendln(">>" );
00060               endObject(); 
00061               newObject();
00062               appendln("<< /Type /Outlines");
00063               appendln("   /Count 0");
00064               appendln(">>");
00065               endObject();
00066               newObject();
00067               appendln("<< /Type /Pages");
00068               appendln("   /Kids [4 0 R]");
00069               appendln("   /Count 1");
00070               appendln(">>");
00071               endObject();
00072               newObject();
00073               appendln("<< /Type /Page");
00074               appendln("   /Parent 3 0 R");
00075               appendln("   /MediaBox [0 0 "+w+" "+h+"]");
00076               appendln("   /Contents 5 0 R");
00077               appendln("   /Resources << /ProcSet 6 0 R");
00078               appendln("                 /Font << /F1 7 0 R");
00079               appendln("                          /F2 7 0 R " +
00080                      "                                                       /F3 7 0 R >>");
00081               appendln("              >>");
00082               appendln(">>");
00083               endObject();         
00084               newObject();
00085               stream=new StringBuffer();
00086        }
00087 
00088        public void close ()
00089        {      appendln("<< /Length "+stream.length()+" >>");
00090               prefix=""; appendln("stream");
00091               buf.append(stream.toString());
00092               appendln("endstream");
00093               endObject();
00094               newObject();
00095               appendln("[/PDF]");
00096               endObject();
00097               newObject();
00098               appendln("<< /Type /Font");
00099               appendln("   /Subtype /Type1");
00100               appendln("   /Name /F1");
00101               appendln("   /BaseFont /Helvetica");
00102               appendln("   /Encoding /WinAnsiEncoding");
00103               appendln(">>");
00104               endObject();
00105               newObject();
00106               appendln("<< /Type /Font");
00107               appendln("   /Subtype /Type1");
00108               appendln("   /Name /F2");
00109               appendln("   /BaseFont /Symbol");
00110               appendln("   /Encoding /WinAnsiEncoding");
00111               appendln(">>");
00112               endObject();
00113               newObject();
00114               appendln("<< /Type /Font");
00115               appendln("   /Subtype /Type1");
00116               appendln("   /Name /F3");
00117               appendln("   /BaseFont /Helvetica-Bold");
00118               appendln("   /Encoding /WinAnsiEncoding");
00119               appendln(">>");
00120               endObject();
00121               appendln("");
00122               appendln("xref");
00123               int xrefstart=buf.length();
00124               appendln(0+" "+nxref);
00125               buf.append(xref.toString());
00126               appendln("");
00127               appendln("trailer");
00128               appendln("<< /Size "+nxref);
00129               appendln("   /Root 1 0 R");
00130               appendln(">>");
00131               appendln("");
00132               appendln("startxref");
00133               appendln(""+xrefstart);
00134               appendln("");
00135               appendln("%%EOF");
00136               Out.write(buf.toString());  
00137        }
00138        
00139        public void appendln (String s)
00140        {      buf.append(prefix+s);
00141               buf.append((char)13); buf.append((char)10);
00142        }
00143        
00144        public void streamln (String s)
00145        {      stream.append(prefix+s);
00146               stream.append((char)13); stream.append((char)10);
00147        }
00148        
00149        public void appendxref (int offset, int number, boolean present)
00150        {      xref.append(format(offset,10)+" "+format(number,5)+(present?" n":" f"));
00151               xref.append((char)13); xref.append((char)10);
00152        }
00153 
00154        public String format (int n, int size)
00155        {      String s=""+n;
00156               while (s.length()<size) s="0"+s;
00157               return s;
00158        }
00159        
00160        public void newObject ()
00161        {      int n=buf.length();
00162               appendln("");
00163               appendln(nxref+" "+0+" obj");
00164               appendxref(n,0,true);
00165               nxref++;
00166               prefix="  ";
00167        }
00168        
00169        public void endObject ()
00170        {      prefix="";
00171               appendln("endobj");
00172        }
00173 
00174        public double r (double x)
00175        {      return Math.round(x*100.0)/100.0;
00176        }
00177        
00178        public double ry (double y)
00179        {      return Math.round((H-y)*100.0)/100.0;
00180        }
00181        
00182        public void setStroke (int stroke)
00183        {      if (Stroke==stroke) return;
00184               Stroke=stroke;
00185               switch (Stroke)
00186               {      case Normal :
00187                             streamln(r(LineWidth)+" w"); 
00188                             streamln("[] 0 d");
00189                             break;
00190                      case Thick :
00191                             streamln(r(3*LineWidth)+" w"); 
00192                             streamln("[] 0 d");
00193                             break;
00194                      case Thin :
00195                             streamln(LineWidth+" w");
00196                             streamln("["+r(3*LineWidth)+" "+r(3*LineWidth)+"] 0 d");
00197                      break;
00198               }
00199        
00200        }
00201        
00202        public void clearRect (int x, int y, int w, int h, Color c){}
00203 
00204        Color OldColor=null;
00205 
00206        public void setColor (Color c)
00207        {      if (OldColor!=null && 
00208                      c.getRed()==OldColor.getRed() &&
00209                      c.getGreen()==OldColor.getGreen() &&
00210                      c.getBlue()==OldColor.getBlue()) return;
00211               streamln(
00212                      r(c.getRed()/255.0)+" "+
00213                      r(c.getGreen()/255.0)+" "+
00214                      r(c.getBlue()/255.0)+" rg");
00215               OldColor=c;
00216        }
00217        
00218        public void setColor (ConstructionObject o)
00219        {      if (o.isJobTarget()) setColor(ZirkelFrame.TargetColor);
00220               else if (o.indicated()) setColor(ZirkelFrame.IndicateColor);
00221               else if (o.selected()) setColor(ZirkelFrame.SelectColor);
00222               else
00223               {      if (o.getColorType()==ConstructionObject.THIN)
00224                      {      int i=o.getColorIndex();
00225                             if (o.isHidden()) setColor(ZirkelFrame.BrighterLightColors[i]);
00226                             else setColor(ZirkelFrame.LightColors[i]);                     
00227                      }             
00228                      else
00229                      {      int i=o.getColorIndex();
00230                             if (o.isHidden()) setColor(ZirkelFrame.BrighterColors[i]);
00231                             else setColor(ZirkelFrame.Colors[i]);                   
00232                      }
00233               }
00234               if (o.getColorType()==ConstructionObject.THIN)
00235               {      setStroke(Thin);
00236               }
00237               else if (o.getColorType()==ConstructionObject.THICK)
00238               {      setStroke(Thick);
00239               }
00240               else
00241               {      setStroke(Normal);
00242               }
00243        }
00244 
00245        Color OldFillColor=null;
00246 
00247        public void setFillColor (Color c)
00248        {      if (OldFillColor!=null && 
00249                      c.getRed()==OldFillColor.getRed() &&
00250                      c.getGreen()==OldFillColor.getGreen() &&
00251                      c.getBlue()==OldFillColor.getBlue()) return;
00252               streamln(
00253                      r(c.getRed()/255.0)+" "+
00254                      r(c.getGreen()/255.0)+" "+
00255                      r(c.getBlue()/255.0)+" rg");
00256               OldFillColor=c;
00257        }
00258        
00259        public void setFillColor (ConstructionObject o)
00260        {      setStroke(Normal);
00261               if (o.isJobTarget()) setColor(ZirkelFrame.TargetColor);
00262               else if ((o instanceof PointObject) && o.indicated())
00263                      setFillColor(ZirkelFrame.IndicateColor);
00264               else
00265               {      if (o.getColorType()!=ConstructionObject.THICK)
00266                      {      int i=o.getColorIndex();
00267                             if (o.isHidden()) setFillColor(ZirkelFrame.BrighterLightColors[i]);
00268                             else setFillColor(ZirkelFrame.LightColors[i]);                 
00269                      }             
00270                      else
00271                      {      int i=o.getColorIndex();
00272                             if (o.isHidden()) setFillColor(ZirkelFrame.BrighterColors[i]);
00273                             else setFillColor(ZirkelFrame.Colors[i]);               
00274                      }
00275               }
00276        }
00277 
00278        public void setLabelColor (ConstructionObject o)
00279        {      if (o.labelSelected()) setFillColor(ZirkelFrame.SelectColor);
00280               else if (o.isFilled())
00281               {      int type=o.getColorType();
00282                      o.setColorType(ConstructionObject.NORMAL);
00283                      int i=o.getColorIndex();
00284                      setFillColor(ZirkelFrame.Colors[i]);                    
00285                      o.setColorType(type);
00286               }
00287               else
00288               {      int type=o.getColorType();
00289                      o.setColorType(ConstructionObject.NORMAL);
00290                      int i=o.getColorIndex();
00291                      if (o.isHidden()) setFillColor(ZirkelFrame.BrighterColors[i]);
00292                      else setFillColor(ZirkelFrame.Colors[i]);               
00293                      o.setColorType(type);
00294               }
00295        }
00296        
00297        public void drawRect (double x, double y, double w, double h)
00298        {      streamln(r(x)+" "+ry(y+h)+" "+r(w)+" "+r(h)+" "+"re");
00299               streamln("S");
00300        }
00301        
00302        public void drawLine (double x, double y, double x1, double y1, 
00303               ConstructionObject o)
00304        {      streamln(r(x)+" "+ry(y)+" m");
00305               streamln(r(x1)+" "+ry(y1)+" l");
00306               streamln("S");
00307        }
00308        
00309        public void drawLine (double x, double y, double x1, double y1)
00310        {      setStroke(Normal);
00311               streamln(r(x)+" "+ry(y)+" m");
00312               streamln(r(x1)+" "+ry(y1)+" l");
00313               streamln("S");
00314        }
00315        
00316        public void drawThickLine (double x, double y, double x1, double y1)
00317        {      setStroke(Thick);
00318               streamln(r(x)+" "+ry(y)+" m");
00319               streamln(r(x1)+" "+ry(y1)+" l");
00320               streamln("S");
00321               setStroke(Normal);
00322        }
00323 
00324        public void drawArc (double x, double y, double r, double a, double b)
00325        {      double f=r/Math.cos(b/3);
00326               streamln(r(x+Math.cos(a+b/3)*f)+" "+ry(y-Math.sin(a+b/3)*f)+" "+
00327                      r(x+Math.cos(a+2*b/3)*f)+" "+ry(y-Math.sin(a+2*b/3)*f)+" "+
00328                      r(x+Math.cos(a+b)*r)+" "+ry(y-Math.sin(a+b)*r)+" c");                 
00329        }
00330 
00331        public void drawArc (double x, double y, double w, double h, double a, double b)
00332        {      setStroke(Normal);
00333               double r=w/2;
00334               x+=r; y+=r;
00335               a=a/180*Math.PI; b=b/180*Math.PI;
00336               int n=(int)(r*b/10);
00337               if (n<4) n=4;
00338               streamln(r(x+Math.cos(a)*r)+" "+ry(y-Math.sin(a)*r)+" m");
00339               for (int i=0; i<n; i++)
00340               {      drawArc(x,y,r,a+i*b/n,b/n);
00341               }
00342               streamln("S");
00343        }
00344 
00345        public void drawArc (double x, double y, double w, double h, double a, double b,
00346               ConstructionObject o)
00347        {      double r=w/2;
00348               x+=r; y+=r;
00349               a=a/180*Math.PI; b=b/180*Math.PI;
00350               int n=(int)(r*b/10);
00351               if (n<4) n=4;
00352               streamln(r(x+Math.cos(a)*r)+" "+ry(y+-Math.sin(a)*r)+" m");
00353               for (int i=0; i<n; i++)
00354               {      drawArc(x,y,r,a+i*b/n,b/n);
00355               }
00356               streamln("S");
00357        }
00358        
00359        public void setFont (int size, boolean bold)
00360        {      Bold=bold;
00361               FM=new PdfFontMetrics(size); 
00362               streamln("/F1 "+size+" Tf");
00363        }
00364 
00365        public FontMetrics getFontMetrics ()
00366        {      if (FM==null) FM=new PdfFontMetrics(20); 
00367               return FM; 
00368        }
00369 
00370        public void drawString (String s, double x, double y)
00371        {      if (s.length()==1)
00372               {      char c=s.charAt(0);
00373                      for (int i=0; i<Translation.length/2; i++)
00374                      {      if (Translation[2*i+1]==c)
00375                             {      streamln("BT");
00376                                    streamln("/F2 "+FM.Size+" Tf");
00377                                    streamln(r(x)+" "+ry(y)+" Td");
00378                                    streamln("("+Translation[2*i]+") Tj");
00379                                    streamln("ET");
00380                                    return;
00381                             } 
00382                      }
00383               }
00384               streamln("BT");
00385               if (Bold) streamln("/F3 "+FM.Size+" Tf");
00386               else streamln("/F1 "+FM.Size+" Tf");
00387               streamln(r(x)+" "+ry(y)+" Td");
00388               streamln("("+s+") Tj");
00389               streamln("ET");
00390        }
00391 
00392        public static char Translation[]=
00393        {      'a','\u03B1','A','\u0391',
00394               'b','\u03B2','B','\u0392',
00395               'c','\u03B3','C','\u0393',
00396               'd','\u03B4','D','\u0394',
00397               'e','\u03B5','E','\u0395',
00398               'f','\u03D5','F','\u03A6',
00399               'g','\u03B3','G','\u0393',
00400               'h','\u03B7','H','\u0397',
00401               'i','\u03B9','I','\u0399',
00402               'k','\u03BA','K','\u039A',
00403               'l','\u03BB','L','\u039B',
00404               'm','\u03BC','M','\u039C',
00405               'n','\u03BD','N','\u039D',
00406               'o','\u03BF','O','\u03A9',
00407               'p','\u03C0','P','\u03A0',
00408               'q','\u03C7','Q','\u03A7',
00409               'r','\u03C1','R','\u03A1',
00410               's','\u03C3','S','\u03A3',
00411               't','\u03C4','T','\u03A4',
00412               'u','\u03C5','U','\u03A5',
00413               'v','\u03C8','V','\u03A8',
00414               'w','\u03C9','W','\u03A9',
00415               'x','\u03BE','X','\u039E',
00416               'y','\u03C7','Y','\u03A7',
00417               'z','\u03B6','Z','\u0396',         
00418        };
00419 
00420        public void drawOval (double x, double y, double w, double h)
00421        {      drawArc(x,y,w,h,0,360);
00422        }
00423 
00424        public void drawOval (double x, double y, double w, double h,
00425               ConstructionObject o)
00426        {      drawArc(x,y,w,h,0,360,o);
00427        }
00428 
00429        public void fillRect (double x, double y, double w, double h, 
00430               boolean outline, boolean transparent, ConstructionObject o)
00431        {      setFillColor(o);
00432               if (outline) setColor(o);
00433               streamln(r(x)+" "+ry(y+h)+" "+r(w)+" "+r(h)+" "+"re");
00434               streamln(outline?"b":"f");
00435        }
00436 
00437        public void fillOval (double x, double y, double w, double h, 
00438               boolean outline, boolean transparent, ConstructionObject o)
00439        {      fillArc(x,y,w,h,0,360,outline,transparent,true,o);
00440        }
00441               
00442        public void fillPolygon (double x[], double y[], int n,
00443               boolean outline, boolean tranparent, ConstructionObject o)
00444        {      setFillColor(o);
00445               if (outline) setColor(o);
00446               streamln(r(x[0])+" "+ry(y[0])+" m");
00447               for (int i=1; i<n; i++)
00448               {      streamln(r(x[i])+" "+ry(y[i])+" l");
00449               }
00450               streamln(outline?"b*":"f*");
00451        }
00452 
00453 
00454        public void fillArc (double x, double y, double w, double h, double a, double b,
00455               boolean outline, boolean transparent, boolean arc, ConstructionObject o)
00456        {      setFillColor(o);
00457               if (outline) setColor(o);
00458               double r=w/2;
00459               x+=r; y+=r;
00460               a=a*Math.PI/180; b=b*Math.PI/180;
00461               int n=(int)(r*b/10);
00462               if (n<4) n=4;
00463               streamln(r(x+Math.cos(a)*r)+" "+ry(y-Math.sin(a)*r)+" m");
00464               for (int i=0; i<n; i++)
00465               {      drawArc(x,y,r,a+i*b/n,b/n);
00466               }
00467               if (arc)
00468               {      streamln(r(x)+" "+ry(y)+" l");
00469               }
00470               streamln(outline?"b":"f");
00471        }
00472 
00473        public void drawImage (Image i, int x, int y, ImageObserver o){}
00474        public void drawImage (Image i, int x, int y, int w, int h,
00475               ImageObserver o){}
00476 
00477        public static void main (String args[])
00478        {      try
00479               {      ConstructionObject o=new ConstructionObject(new Construction());
00480                      PrintWriter out=new PrintWriter(
00481                             new FileOutputStream("test.pdf"));
00482                      MyGraphicsPDF pdf=new MyGraphicsPDF(out,1000,1000,1);
00483                      pdf.streamln("1 0 0 -1 0 1000 cm");
00484                      pdf.drawRect(300,300,400,400);
00485                      for (int i=0; i<180; i++)
00486                      {      double s=i/180.0*Math.PI,t=(i+1)/180.0*Math.PI;
00487                             pdf.drawLine(500+Math.cos(s)*200,500+Math.sin(s)*200,
00488                                    500+Math.cos(s)*200,500+Math.sin(s)*200);
00489                      }
00490                      pdf.setColor(Color.red);
00491                      pdf.drawArc(-300,-300,1000,1000,0,360);
00492                      pdf.close();
00493                      out.close();
00494               }
00495               catch (Exception e)
00496               {      System.out.println(e);
00497               }
00498        }
00499 
00500        int fsize;
00501        boolean flarge,fbold;
00502        int ffactor=Global.getParameter("ffactor",130);
00503        
00504        public void setDefaultFont (int h, boolean large, boolean bold)
00505        {      fsize=h; flarge=large; fbold=bold;
00506               ffactor=Global.getParameter("ffactor",130);
00507               setFont(large,bold);
00508        }
00509        
00510        public void setFont (boolean large, boolean bold)
00511        {      int size=fsize;
00512               if (large) size=size*ffactor/100;
00513               if (flarge) size=size*ffactor/100;
00514               setFont(size,bold || fbold);
00515        }
00516 
00517        public void drawImage (Image i, double x, double y, double x1, double y1, 
00518                      double x2, double y2, ImageObserver o)
00519        {
00520        }
00521 
00522        public Graphics getGraphics() {
00523               return null;
00524        }
00525 
00526        public int stringWidth(String s) 
00527        {      return getFontMetrics().stringWidth(s);
00528        }
00529 
00530        public int stringHeight(String s) 
00531        {      return getFontMetrics().getHeight();
00532        }
00533 
00534        public int drawStringExtended(String s, double x, double y) 
00535        {      drawString(s,x,y+getFontMetrics().getAscent());
00536               return getFontMetrics().getHeight(); 
00537        }
00538 
00539        public int stringAscent(String s) 
00540        {      return getFontMetrics().getAscent();
00541        }
00542 
00543 }
00544