OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 181 Rev 183
Line 22... Line 22...
22
import org.w3c.dom.Document;
22
import org.w3c.dom.Document;
23
import org.w3c.dom.Element;
23
import org.w3c.dom.Element;
24
import org.xml.sax.SAXException;
24
import org.xml.sax.SAXException;
25
 
25
 
26
public class GraphicsPL {
26
public class GraphicsPL {
27
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>";
27
    private String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>";
28
    Document doc;
28
    private Document doc;
29
    private File imgDir;
29
    private File imgDir;
30
 
30
 
31
    public static void main(String[] args) throws Exception {
31
    public static void main(String[] args) throws Exception {
32
        GraphicsPL g = new GraphicsPL();
32
        GraphicsPL g = new GraphicsPL();
33
        g.load(new File("Template/Labels/test.graphicspl"));
33
        g.load(new File("Template/Labels/test.graphicspl"));
Line 73... Line 73...
73
                float ratio = (printRatio * dpi) / 72f;
73
                float ratio = (printRatio * dpi) / 72f;
74
                try {
74
                try {
75
                    final BufferedImage img = createImage(ratio);
75
                    final BufferedImage img = createImage(ratio);
76
                    g2d.drawImage(img, (int) Math.round(pf.getImageableX()), (int) Math.round(pf.getImageableY()), width, height, null);
76
                    g2d.drawImage(img, (int) Math.round(pf.getImageableX()), (int) Math.round(pf.getImageableY()), width, height, null);
77
                } catch (ParserConfigurationException | SAXException | IOException e) {
77
                } catch (ParserConfigurationException | SAXException | IOException e) {
-
 
78
                    e.printStackTrace();
78
                    throw new PrinterException(e.getMessage());
79
                    throw new PrinterException(e.getMessage());
79
                }
80
                }
80
                return PAGE_EXISTS;
81
                return PAGE_EXISTS;
81
            }
82
            }
82
        };
83
        };
83
    }
84
    }
84
 
85
 
85
    private String getZPL() throws IOException {
86
    public String getZPL() throws IOException {
86
        final ZPLRenderer renderer = new ZPLRenderer();
87
        final ZPLRenderer renderer = new ZPLRenderer();
87
        renderer.render(this);
88
        renderer.render(this);
88
        return renderer.getZPL();
89
        return renderer.getZPL();
89
    }
90
    }
90
 
91
 
91
    private BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException {
92
    public BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException {
92
        final Element root = this.doc.getDocumentElement();
93
        final Element root = this.doc.getDocumentElement();
93
        final int width = Math.round(ratio * Integer.parseInt(root.getAttribute("width")));
94
        final int width = Math.round(ratio * Integer.parseInt(root.getAttribute("width")));
94
        final int height = Math.round(ratio * Integer.parseInt(root.getAttribute("height")));
95
        final int height = Math.round(ratio * Integer.parseInt(root.getAttribute("height")));
95
        final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
96
        final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
96
        final Graphics2D graphics = (Graphics2D) img.getGraphics();
97
        final Graphics2D graphics = (Graphics2D) img.getGraphics();
Line 104... Line 105...
104
 
105
 
105
    public Document getDocument() {
106
    public Document getDocument() {
106
        return this.doc;
107
        return this.doc;
107
    }
108
    }
108
 
109
 
109
    private void load(File file) throws ParserConfigurationException, SAXException, IOException {
110
    public void load(File file) throws ParserConfigurationException, SAXException, IOException {
110
        this.xml = new String(Files.readAllBytes(file.toPath()));
111
        this.xml = new String(Files.readAllBytes(file.toPath()));
111
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
112
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
112
        // factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
113
        // factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
113
        final DocumentBuilder builder = factory.newDocumentBuilder();
114
        final DocumentBuilder builder = factory.newDocumentBuilder();
114
        final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
115
        final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
115
        this.doc = builder.parse(input);
116
        this.doc = builder.parse(input);
116
        this.doc.getDocumentElement().normalize();
117
        this.doc.getDocumentElement().normalize();
117
        this.imgDir = file.getParentFile();
118
        this.imgDir = file.getParentFile();
118
    }
119
    }
119
 
120
 
-
 
121
    public void load(String str, File imageDir) throws ParserConfigurationException, SAXException, IOException {
-
 
122
        this.xml = str;
-
 
123
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-
 
124
        // factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
-
 
125
        final DocumentBuilder builder = factory.newDocumentBuilder();
-
 
126
        final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
-
 
127
        this.doc = builder.parse(input);
-
 
128
        this.doc.getDocumentElement().normalize();
-
 
129
        this.imgDir = imageDir;
-
 
130
    }
-
 
131
 
120
    public File getImageDir() {
132
    public File getImageDir() {
121
        return this.imgDir;
133
        return this.imgDir;
122
    }
134
    }
123
 
135
 
124
    public void setImageDir(File dir) {
136
    public void setImageDir(File dir) {