Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sforzando.vector;
- public class SVGIO {
- public static SVGImage read (File input) {
- return read(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input));
- }
- public static SVGImage read (InputStream stream) {
- return read(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream));
- }
- public static SVGImage read (URL url) {
- return read(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.toURI().toString()));
- }
- public static SVGImage read (Document dom) {
- NodeList nodes = dom.getElementsByTagName("svg");
- for (int i = 0; i < nodes.getLength(); i++) {
- if (nodes.get(i) instanceof Element) {
- Element svg = (Element) nodes.get(i);
- int width = Integer.parseInt(svg.getAttributeNode("width").getValue());
- int height = Integer.parseInt(svg.getAttributeNode("height").getValue());
- int x = 0;
- int y = 0;
- int w = width;
- int h = height;
- if (svg.getAttributeNode("viewBox") != null) {
- String[] vb = svg.getAttributeNode("viewBox").split(" ");
- x = Integer.parseInt(vb[0]);
- y = Integer.parseInt(vb[1]);
- w = Integer.parseInt(vb[2]);
- h = Integer.parseInt(vb[3]);
- }
- SVGImage img = new SVGImage(width, height, x, y, w, h);
- NodeList nodes2 = svg.getChildNodes();
- for (int i = 0; i < nodes2.getLength(); i++)
- if (nodes2.item(i) instanceof Element)
- img.addComponent(SVGComponentParsers.get(nodes2.item(i).getNodeName()).parse((Element) nodes2.item(i)));
- return img;
- }
- }
- return img;
- }
- public static Document compile (SVGImage img) {
- }
- public static void write (SVGImage img, File output) {
- }
- public static void write (SVGImage img, OutputStream os) {
- }
- }
- public interface SVGComponentParser {
- SVGComponent parse (Element svg);
- }
- public class SVGComponentParsers {
- private HashMap<String, SVGComponentParser> REGISTRY = new HashMap<String, SVGComponentParser>();
- public static void register (String s, SVGComponentParser parser) {
- REGISTRY.put(s, parser);
- }
- public static SVGComponentParser get (String s) {
- return REGISTRY.get(s);
- }
- }
- public class SVGImage {
- public final int x;
- public final int y;
- public final int width;
- public final int height;
- public final int viewportWidth;
- public final int viewportHeight;
- private ArrayList<SVGComponent> components = new ArrayList<SVGComponent>();
- public SVGImage (int width, int height, int x, int y, int w, int h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- this.viewportWidth = width;
- this.viewportHeight = height;
- }
- public void add (SVGComponent comp) {
- this.components.add(comp);
- }
- public BufferedImage render (AntiAliasingType type) {
- BufferedImage img = new BufferedImage((this.width - this.x) * type.level, (this.height - this.y) * type.level, BufferedIMage.TYPE_INT_ARGB);
- this.draw(img.createGraphics(), type);
- return resize(img, type);
- }
- private void draw (Graphics g, AntiAliasingType type) {
- for (SVGComponent c : this.components)
- c.draw(g, type, this.x, this.y, this.viewportWidth, this.viewportHeight);
- }
- private static BufferedImage resize (BufferedImage src, AntiAliasingType type) {
- BufferedImage dest = new BufferedImage(src.getWidth() / type.level, src.getHeight() / type.level);
- Graphics g = dest.createGraphics();
- for (int x = 0; x < dest.getWidth(); x++) {
- for (int y = 0; y < dest.getHeight(); y++) {
- g.setColor(getColor(src, type, x, y));
- dest.fillRect(x, y, 1, 1);
- }
- }
- return dest;
- }
- private static Color getColor (BufferedImage src, AntiAliasingType type, int x, int y) {
- int[] rgb = new int[type.level * type.level];
- src.getRGB(x, y, type.level, type.level, rgb, 0, type.level);
- int avgR = Integer.compress(rgb[0], 0xFF);
- int avgG = Integer.compress(rgb[0], 0xFF00);
- int avgB = Integer.compress(rgb[0], 0xFF0000);
- for (int i = 0; i < rgb.length; i++) {
- int r = Integer.compress(rgb[i], 0xFF);
- avgR = (avgR + r) / 2;
- int g = Integer.compress(rgb[i], 0xFF00);
- avgG = (avgG + g) / 2;
- int b = Integer.compress(rgb[i], 0xFF0000);
- avgB = (avgB + b) / 2;
- }
- return new Color(avgR, avgG, avgB);
- }
- }
- public interface SVGComponent {
- void draw (Graphics g, AntiAliasingType type, CoordinateRemapper m);
- }
- public enum AntiAliasingType {
- NONE(1),
- DOUBLE_MSAA(2),
- TRIPLE_MSAA(3),
- QUAD_SSAA(4);
- final int level;
- private AntiAliasingType (int i) {
- this.level = i;
- }
- }
- package com.sforzando.vector.components;
- public class SVGRectComponent {
- private final double x;
- private final double y;
- private final double w;
- private final double h;
- private final double strokeWidth;
- private final Color stroke;
- private final Color fill;
- public SVGRectComponent (double x, double y, double w, double h) {
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- }
- public void draw (Graphics g, AntiAliasingType type, CoordinateRemapper m) {
- double halfStroke = this.strokeWidth / 2;
- g.setColor(this.stroke);
- g.fillRect(m.remapX(this.x - halfStroke), m.remapY(this.y - halfStroke), m.remapX(this.w + this.strokeWidth), m.remapY(this.h + this.strokeWidth));
- g.setColor(this.stroke);
- g.fillRect(m.remapX(this.x + halfStroke), m.remapY(this.y + halfStroke), m.remapX(this.w), m.remapY(this.h));
- }
- }
Advertisement