View Javadoc

1   // Copyright FreeHEP 2007.
2   package org.freehep.graphicsio.emf;
3   
4   import org.freehep.util.export.ExportFileType;
5   
6   import java.util.List;
7   import java.io.FileInputStream;
8   import java.io.File;
9   
10  /**
11   * This class converts an EMF image to all available
12   * grafik formats, e.g. PDF, or PNG
13   *
14   * @author Steffen Greiffenberg
15   * @version $Id$
16   */
17  public class EMFConverter {
18  
19      /**
20       * Looks for an (FreeHEP-) ExportFileType in class path
21       * to create the selected output format for destFileName.
22       *
23       * @param type extension / file format to write
24       * @param srcFileName emf file to read
25       * @param destFileName file to create, if null srcFileName
26       *        is used with the extension type
27       */
28      protected static void export(String type, String srcFileName, String destFileName) {
29          try {
30              List exportFileTypes = ExportFileType.getExportFileTypes(type);
31              if (exportFileTypes == null || exportFileTypes.size() == 0) {
32                  System.out.println(
33                      type + " library is not available. check your classpath!");
34                  return;
35              }
36  
37              ExportFileType exportFileType = (ExportFileType) exportFileTypes.get(0);
38  
39              // read the EMF file
40              EMFRenderer emfRenderer = new EMFRenderer(
41                  new EMFInputStream(
42                      new FileInputStream(srcFileName)));
43  
44              // create the destFileName,
45              // replace or add the extension to the destFileName
46              if (destFileName == null || destFileName.length() == 0) {
47                  // index of the beginning of the extension
48                  int lastPointIndex = srcFileName.lastIndexOf(".");
49  
50                  // to be sure that the point separates an extension
51                  // and is not part of a directory name
52                  int lastSeparator1Index = srcFileName.lastIndexOf("/");
53                  int lastSeparator2Index = srcFileName.lastIndexOf("\\");
54  
55                  if (lastSeparator1Index > lastPointIndex ||
56                      lastSeparator2Index > lastPointIndex) {
57                      destFileName = srcFileName + ".";
58                  } else if (lastPointIndex > -1) {
59                      destFileName = srcFileName.substring(
60                          0, lastPointIndex + 1);
61                  }
62  
63                  // add the extension
64                  destFileName += type.toLowerCase();
65              }
66  
67              // TODO there is no possibility to use Constants of base class!
68              /* create SVG properties
69              Properties p = new Properties();
70              p.put(SVGGraphics2D.EMBED_FONTS, Boolean.toString(false));
71              p.put(SVGGraphics2D.CLIP, Boolean.toString(true));
72              p.put(SVGGraphics2D.COMPRESS, Boolean.toString(false));
73              p.put(SVGGraphics2D.TEXT_AS_SHAPES, Boolean.toString(false));
74              p.put(SVGGraphics2D.FOR, "Freehep EMF2SVG");
75              p.put(SVGGraphics2D.TITLE, emfFileName);*/
76  
77              EMFPanel emfPanel = new EMFPanel();
78              emfPanel.setRenderer(emfRenderer);
79  
80              // TODO why uses this classes components?!
81              exportFileType.exportToFile(
82                 new File(destFileName),
83                 emfPanel,
84                 emfPanel,
85                 null,
86                 "Freehep EMF converter");
87          } catch (Exception e) {
88              e.printStackTrace();
89          }
90      }
91  
92      /**
93       * starts the conversion
94       *
95       * @param args args[0] source file, args[1] destination
96       *        file with target format in extension
97       */
98      public static void main(String[] args) {
99          try {
100             export(
101                 args[1].substring(args[1].lastIndexOf(".") + 1),
102                 args[0],
103                 args[1]);
104         } catch (Exception e) {
105             System.out.println("usage: EMFConverter imput.emf output.extension");
106         }
107     }
108 }