public GraphicSet importSetFromFile(File inputFile, List<String> warnings) throws ImportException { Writer out = null; try { // Get a DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator final SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.setTransform(new AffineTransform()); // Open input file PSInputFile in = new PSInputFile(inputFile.getAbsolutePath()); Rectangle2D bb = this.getBoundingBox(inputFile); svgGenerator.setTransform(AffineTransform.getTranslateInstance(-bb.getX(), -bb.getY())); Dimension d = new Dimension((int) bb.getWidth(), (int) bb.getHeight()); // Create processor and associate to input and output file Processor processor = new Processor(svgGenerator, d, false); processor.setData(in); // Process processor.process(); File tmp = File.createTempFile("temp", "svg"); tmp.deleteOnExit(); svgGenerator.stream(new FileWriter(tmp)); GraphicSet result = new SVGImporter().importSetFromFile(tmp, warnings); // Assume the EPS has been created with 72DPI (from Inkscape) double px2mm = Util.inch2mm(1d / 72d); result.setBasicTransform(AffineTransform.getScaleInstance(px2mm, px2mm)); return result; } catch (Exception ex) { Logger.getLogger(EPSImporter.class.getName()).log(Level.SEVERE, null, ex); throw new ImportException(ex); } }
private static void exportScreenshotSVG( Component target, File selectedFile, boolean paintOffscreen) throws IOException { String format = "svg"; selectedFile = fixFileExt(selectedFile, new String[] {format}, format); // Create an instance of org.w3c.dom.Document. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, format, null); // Write image data into document SVGGraphics2D svgGenerator = new SVGGraphics2D(document); choosePaint(target, svgGenerator, paintOffscreen); Writer out = null; try { // Finally, stream out SVG to the standard output using // UTF-8 encoding. boolean useCSS = true; // we want to use CSS style attributes out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(selectedFile), "UTF-8")); svgGenerator.stream(out, useCSS); } finally { if (out != null) try { out.close(); } catch (IOException e) { log.error("Error closing svg file", e); } } }
private void saveChartAsSVG(JFreeChart chart, String fileName, int width, int height) throws KeyedException { Writer out = null; try { out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"); String svgNS = "http://www.w3.org/2000/svg"; DOMImplementation di = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 1.0"); Document document = di.createDocument(svgNS, "svg", null); SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); ctx.setEmbeddedFontsOn(true); SVGGraphics2D svgGenerator = new CustomSVGGraphics2D(ctx, true, 100, true); svgGenerator.setSVGCanvasSize(new Dimension(width, height)); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height)); boolean useCSS = true; svgGenerator.stream(out, useCSS); svgGenerator.dispose(); } catch (Exception e) { throw K.JFC_OUTPUT_ERR.exception(e, fileName); } finally { try { if (out != null) out.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Captures the actual state of arena to the output SVG file. * * @param name */ public void captureToSVG(String name) { boolean oldIsAllArenaPartsAntialiased = isAllArenaPartsAntialiased; System.out.println("Capturing scenario to " + name); if (!isSVGGraphicsInitialized) { initializeSVGGraphics(); } setAllArenaPartsAntialiased(false); try { paintUnbuffered(svgGraphics); File outputFile = new File(name); FileWriter out = new FileWriter(outputFile); svgGraphics.stream(out); } catch (Exception ex) { ex.printStackTrace(); } setAllArenaPartsAntialiased(oldIsAllArenaPartsAntialiased); }
/** Produces list of signatures with fixed rate. */ @Scheduled(fixedRate = ProducersConfig.PRODUCTION_RATE) public void produce() { /* Generate random list ID */ String listId = UUID.randomUUID().toString(); /* Filename */ String filename = ProducersConfig.BASE_DIR + listId.toString(); /* Generate random signatures */ List<RandomSignature> signatures = signatureGenerator.randomSignatures(30); /* Render list of signatures to SVG document */ SVGGraphics2D rendered = signatureListSvgRender.render(signatures); try { /* Create the "lock" file for new list */ File lock = new File(filename + ".lock"); lock.createNewFile(); /* Create the SVG file with signatures */ File svg = new File(filename + ".svg"); rendered.stream(new FileWriter(svg), false); /* Convert SVG -> PNG */ svgConverter.setSources(new String[] {filename + ".svg"}); svgConverter.setDst(new File(filename + ".png")); svgConverter.execute(); /* Delete original SVG file */ svg.delete(); /* Write transcriptions of signatures to text file */ FileWriter fileWriter = new FileWriter(new File(filename + ".txt")); for (RandomSignature signature : signatures) { fileWriter.write(signature.toString() + "\n"); } fileWriter.close(); /* Remove the "lock" file and allow to batch process this list */ lock.delete(); } catch (IOException | SVGConverterException e) { e.printStackTrace(); } }
public static void exportSVGImage(PrintWriter pr, Component c) throws IOException { // Get a DOMImplementation. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document. String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); // Create an instance of the SVG Generator. SVGGraphics2D svgGenerator = new SVGGraphics2D(document); c.paint(svgGenerator); // Finally, stream out SVG to the standard output using // UTF-8 encoding. svgGenerator.stream(pr, false); }
/** * Exports a JFreeChart to a SVG file. * * @param chart JFreeChart to export * @param bounds the dimensions of the viewport * @param svgFile the output file. * @throws IOException if writing the svgFile fails. */ public static byte[] convertChartToSVG(JFreeChart chart, int width, int height) { // Get a DOMImplementation and create an XML document DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // draw the chart in the SVG generator chart.draw(svgGenerator, new Rectangle(width, height)); // Write svg file try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Writer out = new OutputStreamWriter(baos, "UTF-8"); svgGenerator.stream(out, true /* use css */); out.flush(); out.close(); return baos.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws Exception { // Create an SVG document. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, "svg", null); // Create a converter for this document. SVGGraphics2D g = new SVGGraphics2D(doc); // Do some drawing. Shape circle = new Ellipse2D.Double(0, 0, 50, 50); g.setPaint(Color.red); g.fill(circle); g.translate(60, 0); g.setPaint(Color.green); g.fill(circle); g.translate(60, 0); g.setPaint(Color.blue); g.fill(circle); g.setSVGCanvasSize(new Dimension(180, 50)); // Populate the document root with the generated SVG content. Element root = doc.getDocumentElement(); g.getRoot(root); Writer out = new OutputStreamWriter(System.out, "UTF-8"); g.stream(out, true); // Display the document. JSVGCanvas canvas = new JSVGCanvas(); JFrame f = new JFrame(); f.getContentPane().add(canvas); canvas.setSVGDocument(doc); f.pack(); f.setVisible(true); }
/** * Do request. * * @param request the request * @param response the response * @throws ServletException the servlet exception * @throws IOException Signals that an I/O exception has occurred. */ private void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String appId = request.getParameter("appId"); String versId = request.getParameter("versId"); // Build the rest client ClientResponse restResponse; MultivaluedMap<String, String> queryParams; String isserver = (String) getServletContext().getAttribute("isserver"); URI uri = UriBuilder.fromUri(isserver + "/rest/").build(); Client client = Client.create(); WebResource service = client.resource(uri); // Issue the request /* restResponse = service.path("application/"+appId+"/"+versId+"/deployment").accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); String deployments = restResponse.getEntity(String.class); System.out.println(deployments); */ // Issue the request restResponse = service .path("application/" + appId + "/" + versId + "/topology") .accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); String descStr = restResponse.getEntity(String.class); JSONObject temp = new JSONObject(descStr); String desc = temp.getString("versTopology"); System.out.println(desc); /* // Build report JSONObject json = new JSONObject(); json.put("desc", new JSONObject(desc)); json.put("depl", new JSONArray(deployments)); response.setContentType("application/json"); response.getWriter().write(json.toString()); */ // Get a DOMImplementation. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document. String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); // Create an instance of the SVG Generator. SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // Ask the test to render into the SVG Graphics2D implementation. try { // TODO // desc is in xml ? can be done with json this.paint(svgGenerator, desc); } catch (Exception e) { e.printStackTrace(); } // Finally, stream out SVG to the standard output using // UTF-8 encoding. boolean useCSS = true; // we want to use CSS style attributes OutputStream outStream = response.getOutputStream(); // Writer out = new OutputStreamWriter(System.out, "UTF-8"); Writer out = new OutputStreamWriter(outStream, "UTF-8"); svgGenerator.stream(out, useCSS); response.setContentType("application/xml"); // response.getWriter().write(svgGenerator.toString()); // response.getWriter().write(outStream.toString()); }
public static void main(String[] args) throws IOException { Context context; S57map map = null; BufferedReader in; int line = 0; String format = ""; String file = ""; String k = ""; String v = ""; BufferedImage img; Graphics2D g2; boolean inIcons = false; boolean inIcon = false; if (args.length < 2) { System.err.println("Usage: java -jar jicons.jar icon_definition_file icons_directory"); System.exit(-1); } in = new BufferedReader(new FileReader(args[0])); context = new Context(); String ln; while ((ln = in.readLine()) != null) { line++; if (inIcons) { if (inIcon) { if (ln.contains("</icon")) { inIcon = false; map.tagsDone(0); // generate icon file switch (format) { case "PNG": img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); g2 = img.createGraphics(); Renderer.reRender( g2, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context); try { ImageIO.write(img, "png", new File(args[1] + file + ".png")); } catch (Exception e) { System.err.println("Line " + line + ": PNG write Exception"); } System.err.println(file + ".png"); break; case "SVG": DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.setSVGCanvasSize(new Dimension(w, h)); Renderer.reRender( svgGenerator, new Rectangle(x, y, w, h), 16, s / Renderer.symbolScale[16], map, context); boolean useCSS = true; Writer out = null; try { out = new OutputStreamWriter( new FileOutputStream(args[1] + file + ".svg"), "UTF-8"); } catch (IOException e1) { System.err.println("Line " + line + ": SVG file Exception"); } try { svgGenerator.stream(out, useCSS); } catch (SVGGraphics2DIOException e) { System.err.println("Line " + line + ": SVG write Exception"); } System.err.println(file + ".svg"); break; } } else if (ln.contains("<tag")) { k = v = ""; String[] token = ln.split("k="); k = token[1].split("[\"\']")[1]; token = token[1].split("v="); v = token[1].split("[\"\']")[1]; if (k.isEmpty()) { System.err.println("Line " + line + ": No key in tag"); System.exit(-1); } if (v.isEmpty()) { System.err.println("Line " + line + ": No value in tag"); System.exit(-1); } map.addTag(k, v); } } else if (ln.contains("<icon")) { inIcon = true; h = w = x = y = -1; s = 0; file = format = ""; map = new S57map(true); map.addNode(0, 0, 0); for (String token : ln.split("[ ]+")) { if (token.matches("^width=.+")) { w = Integer.parseInt(token.split("[\"\']")[1]); } else if (token.matches("^height=.+")) { h = Integer.parseInt(token.split("[\"\']")[1]); } else if (token.matches("^x=.+")) { x = Integer.parseInt(token.split("[\"\']")[1]); } else if (token.matches("^y=.+")) { y = Integer.parseInt(token.split("[\"\']")[1]); } else if (token.matches("^scale=.+")) { s = Double.parseDouble(token.split("[\"\']")[1]); } else if (token.matches("^file=.+")) { file = (token.split("[\"\']")[1]); } else if (token.matches("^format=.+")) { format = (token.split("[\"\']")[1]); } } if (file.isEmpty()) { System.err.println("Line " + line + ": No filename"); System.exit(-1); } if (format.isEmpty()) { System.err.println("Line " + line + ": No format"); System.exit(-1); } if ((h < 0) && (w < 0)) { System.err.println("Line " + line + ": No icon size"); System.exit(-1); } if (w < 0) { w = h; } if (h < 0) { h = w; } if (x < 0) { x = w / 2; } if (y < 0) { y = h / 2; } if (s == 0) { s = 1; } } else if (ln.contains("</icons")) { inIcons = false; break; } } else if (ln.contains("<icons")) { inIcons = true; } } in.close(); System.err.println("Finished"); System.exit(0); }