OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.label.graphicspl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class GraphicsPL {
    private String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>";
    private Document doc;
    private File imgDir;

    public static void main(String[] args) throws Exception {
        GraphicsPL g = new GraphicsPL();
        g.load(new File("Template/Labels/test.graphicspl"));
        BufferedImage img = g.createImage(10);
        ImageIO.write(img, "png", new File("gpl.png"));
        String zpl = g.getZPL();
        System.out.println(zpl);
        Printable p = g.createPrintable();
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(p);
        boolean ok = job.printDialog();
        if (ok) {
            job.print();
        }

    }

    public Printable createPrintable() {
        final Element root = this.doc.getDocumentElement();
        int dpi = 300;
        if (root.hasAttribute("dpi")) {
            dpi = Integer.parseInt(root.getAttribute("dpi"));
        }
        float printRatio = 1f;
        if (root.hasAttribute("printratio")) {
            printRatio = Float.parseFloat(root.getAttribute("printratio"));
        }
        return createPrintable(dpi, printRatio);
    }

    public Printable createPrintable(int dpi, float printRatio) {
        return new Printable() {

            @Override
            public int print(Graphics graphics, PageFormat pf, int pageIndex) throws PrinterException {
                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                }
                final Element root = GraphicsPL.this.doc.getDocumentElement();
                final int width = Math.round(printRatio * Integer.parseInt(root.getAttribute("width")));
                final int height = Math.round(printRatio * Integer.parseInt(root.getAttribute("height")));
                final Graphics2D g2d = (Graphics2D) graphics;
                float ratio = (printRatio * dpi) / 72f;
                try {
                    final BufferedImage img = createImage(ratio);
                    g2d.drawImage(img, (int) Math.round(pf.getImageableX()), (int) Math.round(pf.getImageableY()), width, height, null);
                } catch (ParserConfigurationException | SAXException | IOException e) {
                    e.printStackTrace();
                    throw new PrinterException(e.getMessage());
                }
                return PAGE_EXISTS;
            }
        };
    }

    public String getZPL() throws IOException {
        final ZPLRenderer renderer = new ZPLRenderer();
        renderer.render(this);
        return renderer.getZPL();
    }

    public BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException {
        final Element root = this.doc.getDocumentElement();
        final int width = Math.round(ratio * Integer.parseInt(root.getAttribute("width")));
        final int height = Math.round(ratio * Integer.parseInt(root.getAttribute("height")));
        final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D graphics = (Graphics2D) img.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        final Graphics2DRenderer renderer = new Graphics2DRenderer(graphics, ratio);
        renderer.render(this);
        graphics.dispose();
        return img;
    }

    public Document getDocument() {
        return this.doc;
    }

    public void load(File file) throws ParserConfigurationException, SAXException, IOException {
        this.xml = new String(Files.readAllBytes(file.toPath()));
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
        this.doc = builder.parse(input);
        this.doc.getDocumentElement().normalize();
        this.imgDir = file.getParentFile();
    }

    public void load(String str, File imageDir) throws ParserConfigurationException, SAXException, IOException {
        this.xml = str;
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
        this.doc = builder.parse(input);
        this.doc.getDocumentElement().normalize();
        this.imgDir = imageDir;
    }

    public File getImageDir() {
        return this.imgDir;
    }

    public void setImageDir(File dir) {
        this.imgDir = dir;
    }

}