Пример #1
0
  private void loadURL(URL url) {
    boolean verbose = cmCheck_verbose.isSelected();

    universe = new SVGUniverse();
    universe.setVerbose(verbose);
    SVGDiagram diagram = null;

    if (!CheckBoxMenuItem_anonInputStream.isSelected()) {
      // Load from a disk with a valid URL
      URI uri = universe.loadSVG(url);

      if (verbose) System.err.println(uri.toString());

      diagram = universe.getDiagram(uri);
    } else {
      // Load from a stream with no particular valid URL
      try {
        InputStream is = url.openStream();
        URI uri = universe.loadSVG(is, "defaultName");

        if (verbose) System.err.println(uri.toString());

        diagram = universe.getDiagram(uri);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    svgDisplayPanel.setDiagram(diagram);
    repaint();
  }
Пример #2
0
 public void updateTime(double curTime) {
   try {
     if (universe != null) {
       universe.setCurTime(curTime);
       universe.updateTime();
       //            svgDisplayPanel.updateTime(curTime);
       repaint();
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #3
0
  @Override
  public URI call() throws Exception {
    // System.out.println( "SVGLoad start"+xmlBase.toString());
    SVGLoader handler = new SVGLoader(xmlBase, univ, false);

    // Place this docment in the universe before it is completely loaded
    // so that the load process can refer to references within it's current
    // document
    univ.loadedDocs.put(xmlBase, handler.getLoadedDiagram());
    XMLReader reader = null;
    InputSource is = null;
    try {
      is = input == null ? univ.getInputSource(xmlBase) : input;
      // Parse the input
      reader = univ.getXMLReaderCached();
      reader.setEntityResolver(
          new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) {
              // Ignore all DTDs
              return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
          });
      reader.setContentHandler(handler);
      reader.parse(is);

      for (SVGLoaderClient eachRunnable : doAfter) {
        eachRunnable.imageLoaded(xmlBase);
      }

      //            SAXParser saxParser = factory.newSAXParser();
      //            saxParser.parse(new InputSource(new BufferedReader(is)), handler);
      // System.out.println( "SVGLoad end"+xmlBase.toString());
      return xmlBase;
    } catch (SAXParseException sex) {
      System.err.println("Error processing " + xmlBase);
      System.err.println(sex.getMessage());
      sex.printStackTrace();

      univ.loadedDocs.remove(xmlBase);
      return null;
    } catch (Throwable t) {
      System.err.println(xmlBase.toASCIIString());
      t.printStackTrace();
    } finally {

      SVGUniverse.closeStreamQuietly(is.getByteStream());
      SVGUniverse.closeReaderQuietly(is.getCharacterStream());
    }
    return null;
  }
Пример #4
0
 /**
  * @param pathToSVGFile e.g. "/tracks/monaco/track.svg"
  * @param width int declaring required width
  * @param height int declaring required height
  * @return the successfully rendered BufferedImage, null otherwise
  */
 public static BufferedImage getSVGImage(
     final String pathToSVGFile, final int width, final int height) {
   final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   try {
     final SVGUniverse uni = new SVGUniverse();
     final URI svgURI = uni.loadSVG(ImageLoader.class.getResource(pathToSVGFile));
     paintImage((Graphics2D) image.getGraphics(), new Dimension(width, height), uni, svgURI);
   } catch (final Exception e) {
     e.printStackTrace();
     return null;
   }
   return createCompatibleImage(image, true);
 }
Пример #5
0
  private void loadURL(URL url) {
    boolean verbose = cmCheck_verbose.isSelected();

    //                SVGUniverse universe = new SVGUniverse();
    SVGUniverse universe = SVGCache.getSVGUniverse();
    SVGDiagram diagram = null;
    URI uri;

    if (!CheckBoxMenuItem_anonInputStream.isSelected()) {
      // Load from a disk with a valid URL
      uri = universe.loadSVG(url);

      if (verbose) System.err.println("Loading document " + uri.toString());

      diagram = universe.getDiagram(uri);
    } else {
      // Load from a stream with no particular valid URL
      try {
        InputStream is = url.openStream();
        uri = universe.loadSVG(is, "defaultName");

        if (verbose) System.err.println("Loading document " + uri.toString());
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }
    }
    /*
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bs);
    os.writeObject(universe);
    os.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
    ObjectInputStream is = new ObjectInputStream(bin);
    universe = (SVGUniverse)is.readObject();
    is.close();
    */

    diagram = universe.getDiagram(uri);

    svgDisplayPanel.setDiagram(diagram);
    repaint();
  }
Пример #6
0
 @Override
 protected void realRun() throws IOException, OsmTransferException {
   LatLon center = Main.getProjection().eastNorth2latlon(Main.map.mapView.getCenter());
   scale =
       Settings.getScaleNumerator()
           / Settings.getScaleDivisor()
           / Math.cos(Math.toRadians(center.lat()));
   this.center = projection.latlon2eastNorth(center);
   try {
     SVGUniverse universe = new SVGUniverse();
     universe.setVerbose(Main.pref.getBoolean("importvec.verbose", false));
     for (File f : files) {
       if (f.isDirectory()) continue;
       if (canceled) {
         return;
       }
       SVGDiagram diagram = universe.getDiagram(f.toURI());
       ShapeElement root = diagram.getRoot();
       if (root == null) {
         throw new IOException("Can't find root SVG element");
       }
       Rectangle2D bbox = root.getBoundingBox();
       this.center = this.center.add(-bbox.getCenterX() * scale, bbox.getCenterY() * scale);
       processElement(root, null);
     }
   } catch (IOException e) {
     throw e;
   } catch (Exception e) {
     throw new IOException(e);
   }
   LinkedList<Command> cmds = new LinkedList<>();
   for (Node n : nodes) {
     cmds.add(new AddCommand(n));
   }
   for (Way w : ways) {
     cmds.add(new AddCommand(w));
   }
   Main.main.undoRedo.add(new SequenceCommand("Import primitives", cmds));
 }
Пример #7
0
  private static void paintImage(
      final Graphics2D g, final Dimension dim, final SVGUniverse uni, final URI svgURI) {
    final Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);

    final SVGDiagram diagram = uni.getDiagram(svgURI);
    if (diagram == null) {
      return;
    }

    if (!scaleToFit) {
      try {
        diagram.render(g);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
      } catch (final SVGException e) {
        throw new RuntimeException(e);
      }
      return;
    }

    final int width = dim.width;
    final int height = dim.height;

    final Rectangle2D.Double rect = new Rectangle2D.Double();
    diagram.getViewRect(rect);

    scaleXform.setToScale(width / rect.width, height / rect.height);

    final AffineTransform oldXform = g.getTransform();
    g.transform(scaleXform);

    try {
      diagram.render(g);
    } catch (final SVGException e) {
      throw new RuntimeException(e);
    }

    g.setTransform(oldXform);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
  }
Пример #8
0
 public synchronized Future<URI> start() {
   if (myResult == null) {
     myResult = SVGUniverse.getPool().submit(this);
   }
   return myResult;
 }