SforzandoCF

vector format data

Feb 2nd, 2026 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. public abstract class SVGComponent implements Iterable {
  2.     protected final Map<String, Object[]> properties;
  3.     protected final List<SVGComponent> children;
  4.     protected final SVGComponent parent;
  5.    
  6.     protected SVGComponent (SVGComponent parent) {
  7.         this.properties = new HashMap<String, Object[]>();
  8.         this.children = new ArrayList<SVGComponent>();
  9.         this.parent = parent;
  10.     }
  11.    
  12.     protected SVGComponent (SVGComponent parent, HashMap<String, Object[]> properties, List<SVGComponent> children) {
  13.         this.properties = properties;
  14.         this.children = children;
  15.         this.parent = parent;
  16.     }
  17.    
  18.     public final <T> T[] getProperties (String name, Class<T> c) {
  19.         c = c.arrayType();
  20.         if (this.properties.containsKey(name) && c.isInstance(this.properties.get(name)))
  21.             return c.cast(this.properties.get(name));
  22.         return null;
  23.     }
  24.    
  25.     public final Object[] getProperties (String name) {
  26.         return this.getProperties(name, Object.class);
  27.     }
  28.    
  29.     public final <T> T[] setProperties (String name, T[] ts) {
  30.         return this.properties.put(name, ts);
  31.     }
  32.    
  33.     public final List<SVGComponent> getChildren () {
  34.         return this.children;
  35.     }
  36.    
  37.     public final Iterator<SVGComponent> iterator () {
  38.         return this.children.iterator();
  39.     }
  40.    
  41.     public abstract void draw (Graphics g, int subpixelCount);
  42. }
  43.  
  44. public class SVGComponents {
  45.     private static final HashMap<String, Class<? extends SVGComponent>> map = new HashMap<String, Class<? extends SVGComponent>>();
  46.    
  47.     @CallerSensitive
  48.     void register (String s) {
  49.         map.put(s, StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass());
  50.     }
  51.    
  52.     public Class<? extends SVGComponent> getClass (String s) {
  53.         return map.get(s);
  54.     }
  55. }
  56.  
  57. public class SVGImageReaderSPI extends ImageReaderSpi {
  58.     private static SVGImageReaderSPI instance;
  59.    
  60.     public SVGImageReaderSPI getInstance () {
  61.         return instance;
  62.     }
  63.    
  64.     public boolean canDecodeInput (Object source) {
  65.         DocumentBuilder db = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
  66.         Document dom = null;
  67.         if (source instanceof Document d)
  68.             dom = d;
  69.         else if (source instanceof String s)
  70.             dom = db.parse(new InputSource(new CharArrayReader(s.toCharArray())));
  71.         else if (source instanceof Reader r)
  72.             dom = db.parse(new InputSource(r));
  73.         else if (source instanceof InputStream s)
  74.             dom = db.parse(s);
  75.         if (dom == null) return false;
  76.         return dom.getElementsByTagName("svg").getLength() != null;
  77.     }
  78.    
  79.     public ImageReader createInstance (Object extension) {
  80.         return new SVGImageReader();
  81.     }
  82.    
  83.     public String[] getImageWriterSpiNames () {
  84.         return new String[] { "com.sforzando.svg.spi.SVGImageWriterSPI" };
  85.     }
  86.    
  87.     public Class<?>[] getInputTypes () {
  88.         return new Class<?>[] {
  89.             Document.class,
  90.             String.class,
  91.             Reader.class,
  92.             InputStream.class };
  93.     }
  94.    
  95.     public boolean isOwnReader (ImageReader reader) {
  96.         return ImageReader instanceof SVGImageReader;
  97.     }
  98. }
  99.  
  100. public class SVGImageReader extends ImageReader {
  101.     private SVGImage result = null;
  102.    
  103.     public SVGImageReader () {
  104.         super (SVGImageReaderSPI.getInstance());
  105.     }
  106.    
  107.     public String getFormatName () {
  108.         return "svg";
  109.     }
  110.    
  111.     public int getHeight (int image) {
  112.         if (image != 0) throw new IndexOutOfBoundsException("svg image reader queried image " + image + " when svg only supports one image");
  113.         Object lock = new Object();
  114.         synchonized (lock) {
  115.             while (this.result == null)
  116.                 try {
  117.                     lock.wait(5);
  118.                 } catch (InterruptedException ie) {}
  119.         }
  120.         return this.result.getPreferredHeight();
  121.     }
  122.    
  123.     public int getNumImages (boolean bool) {
  124.         return 1;
  125.     }
  126.    
  127.     public int getWidth (int image) {
  128.         if (image != 0) throw new IndexOutOfBoundsException("svg image reader queried image " + image + " when svg only supports one image");
  129.         Object lock = new Object();
  130.         synchonized (lock) {
  131.             while (this.result == null)
  132.                 try {
  133.                     lock.wait(5);
  134.                 } catch (InterruptedException ie) {}
  135.         }
  136.         return this.result.getPreferredWidth();
  137.     }
  138.    
  139.     public BufferedImage read (int image, ImageReadParam param) {
  140.         if (image != 0) throw new IndexOutOfBoundsException("svg image reader queried image " + image + " when svg only supports one image");
  141.         Object lock = new Object();
  142.         synchonized (lock) {
  143.             while (this.result == null)
  144.                 try {
  145.                     lock.wait(5);
  146.                 } catch (InterruptedException ie) {}
  147.         }
  148.         BufferedImage img = new BufferedImage(this.result.getPreferredWidth(), this.result.getPreferredHeight(), BufferedImage.TYPE_INT_ARGB);
  149.         return this.result.draw(img.createGraphics(), 4);
  150.     }
  151.    
  152.     public void setInput (Object input, boolean sfo, boolean im) {
  153.         super.setInput(input, sfo, im);
  154.         Thread.startVirtualThread(this::_read);
  155.     }
  156.    
  157.     private void _read () {
  158.         Object source = this.getInput();
  159.         Document dom = null;
  160.         if (source instanceof Document d)
  161.             dom = d;
  162.         else if (source instanceof String s)
  163.             dom = db.parse(new InputSource(new CharArrayReader(s.toCharArray())));
  164.         else if (source instanceof Reader r)
  165.             dom = db.parse(new InputSource(r));
  166.         else if (source instanceof InputStream s)
  167.             dom = db.parse(s);
  168.         if (dom == null) return;
  169.         NodeList svgs = dom.getElementsByTagName("svg");
  170.         if (svgs.getLength() != 1 || !(svgs.item(0) instanceof Element)) return;
  171.         SVGImage img = SVGImage.parse((Element) svgs.item(0));
  172.         if (this.getInput() == source) this.result = img;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment