OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
181 ilm 1
package org.openconcerto.modules.label.graphicspl;
2
 
3
import java.awt.Color;
4
import java.awt.image.BufferedImage;
5
import java.io.File;
6
import java.io.IOException;
7
 
8
import javax.imageio.ImageIO;
9
 
10
import org.w3c.dom.Document;
11
import org.w3c.dom.Element;
12
import org.w3c.dom.Node;
13
import org.w3c.dom.NodeList;
14
 
15
public abstract class GPLRenderer {
16
    public static final int ALIGN_LEFT = 0;
17
    public static final int ALIGN_RIGHT = 1;
18
    public static final int ALIGN_CENTER = 2;
19
 
20
    public static final int BARCODE_EAN8 = 0;
21
    public static final int BARCODE_EAN13 = 1;
22
    public static final int BARCODE_CODE128 = 2;
23
    public static final int BARCODE_CODE128_GS1 = 3;
24
    public static final int BARCODE_DATAMATRIX = 4;
25
    public static final int BARCODE_QRCODE = 5;
26
    private final float ratio;
27
 
28
    public GPLRenderer(float ratio) {
29
        this.ratio = ratio;
30
    }
31
 
32
    public GPLRenderer() {
33
        this.ratio = 1.0f;
34
    }
35
 
36
    public float getRatio() {
37
        return ratio;
38
    }
39
 
40
    public void render(GraphicsPL graphicsPL) throws IOException {
41
 
42
        Document doc = graphicsPL.getDocument();
43
        final int width = Integer.parseInt(doc.getDocumentElement().getAttribute("width"));
44
        NodeList nodeList = doc.getFirstChild().getChildNodes();
45
        int size = nodeList.getLength();
46
        for (int i = 0; i < size; i++) {
47
 
48
            if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
183 ilm 49
                Element element = (Element) nodeList.item(i);
181 ilm 50
                String name = element.getNodeName();
51
                if (name.equals("text")) {
52
                    int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x")));
53
                    int y = Math.round(ratio * Integer.parseInt(element.getAttribute("y")));
54
                    String txt = element.getTextContent();
55
                    Color color = Color.BLACK;
56
                    if (element.hasAttribute("color")) {
57
                        color = Color.decode(element.getAttribute("color"));
58
                    }
59
                    int fontSize = Math.round(ratio * Integer.parseInt(element.getAttribute("fontsize")));
60
                    String fontName = element.getAttribute("font");
61
                    int maxWidth = width - x;
62
                    boolean wrap = false;
63
                    int align = ALIGN_LEFT;
64
                    if (element.hasAttribute("align")) {
65
                        if (element.getAttribute("align").equals("right")) {
66
                            align = ALIGN_RIGHT;
67
                        } else if (element.getAttribute("align").equals("center")) {
68
                            align = ALIGN_CENTER;
69
                        }
70
                    }
71
                    if (!element.hasAttribute("visible") || element.getAttribute("visible").equals("true")) {
72
                        drawText(x, y, txt, align, fontName, fontSize, color, maxWidth, wrap);
73
                    }
74
 
75
                } else if (name.equals("rectangle")) {
76
                    int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x")));
77
                    int y = Math.round(ratio * Integer.parseInt(element.getAttribute("y")));
78
                    int w = Math.round(ratio * Integer.parseInt(element.getAttribute("width")));
79
                    int h = Math.round(ratio * Integer.parseInt(element.getAttribute("height")));
80
                    Color color = Color.BLACK;
81
                    if (element.hasAttribute("color")) {
82
                        color = Color.decode(element.getAttribute("color"));
83
                    }
84
 
85
                    if (element.hasAttribute("fill") && element.getAttribute("fill").equals("true")) {
86
                        fillRectangle(x, y, w, h, color);
87
                    } else {
88
                        int lineWidth = Math.round(ratio);
89
                        if (element.hasAttribute("linewidth")) {
90
                            lineWidth = Math.round(ratio * Integer.parseInt(element.getAttribute("linewidth")));
91
                        }
92
                        drawRectangle(x, y, w, h, color, lineWidth);
93
                    }
94
 
95
                } else if (name.equals("image")) {
96
                    String fileName = element.getAttribute("file");
97
                    int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x")));
98
                    int y = Math.round(ratio * Integer.parseInt(element.getAttribute("y")));
183 ilm 99
                    final File input = new File(graphicsPL.getImageDir(), fileName);
100
                    if (input.exists()) {
101
                        BufferedImage img = ImageIO.read(input);
102
                        int w = Math.round(ratio * img.getWidth());
103
                        int h = Math.round(ratio * img.getHeight());
104
                        if (element.hasAttribute("width")) {
105
                            w = Math.round(ratio * Integer.parseInt(element.getAttribute("width")));
106
                        }
107
                        if (element.hasAttribute("height")) {
108
                            h = Math.round(ratio * Integer.parseInt(element.getAttribute("height")));
109
                        }
110
                        drawImage(x, y, w, h, img);
111
                    } else {
112
                        throw new IllegalStateException(input.getAbsolutePath() + " not found");
181 ilm 113
                    }
114
 
115
                } else if (name.equals("barcode")) {
116
                    int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x")));
117
                    int y = Math.round(ratio * Integer.parseInt(element.getAttribute("y")));
118
                    int h = 0;
119
 
120
                    if (element.hasAttribute("height")) {
121
                        h = Math.round(ratio * Integer.parseInt(element.getAttribute("height")));
122
                    }
123
                    String type = element.getAttribute("type");
124
                    String code = element.getTextContent();
125
                    int t;
126
                    if (type.equals("ean8")) {
127
                        t = BARCODE_EAN8;
128
                    } else if (type.equals("ean13")) {
129
                        t = BARCODE_EAN13;
130
                    } else if (type.equals("ean128")) {
131
                        t = BARCODE_CODE128;
132
                    } else if (type.equals("gs1")) {
133
                        t = BARCODE_CODE128_GS1;
134
                    } else if (type.equals("datamatrix")) {
135
                        t = BARCODE_DATAMATRIX;
136
                        if (h != 0) {
137
                            System.err.println("ignoring datamatrix height attribute");
138
                        }
139
                    } else if (type.equals("qrcode")) {
140
                        t = BARCODE_QRCODE;
141
                    } else {
142
                        throw new IllegalArgumentException("unsupported barcode type : " + type);
143
                    }
144
 
145
                    int fontSize = Math.round(ratio * 8);
146
                    if (element.hasAttribute("fontsize")) {
147
                        fontSize = Math.round(ratio * Integer.parseInt(element.getAttribute("fontsize")));
148
                    }
149
                    int moduleWidth = Math.round(ratio);
150
                    if (element.hasAttribute("modulewidth")) {
151
                        moduleWidth = Math.round(ratio * Integer.parseInt(element.getAttribute("modulewidth")));
152
                    }
153
                    drawBarcode(x, y, h, t, code, moduleWidth, fontSize);
154
                } else {
155
                    throw new IllegalStateException("unsupported primitive : " + name);
156
                }
157
            }
158
        }
159
    }
160
 
161
    public abstract void drawText(int x, int y, String text, int align, String fontName, int fontSize, Color color, int maxWidth, boolean wrap);
162
 
163
    public abstract void drawImage(int x, int y, int w, int h, BufferedImage img);
164
 
165
    public abstract void fillRectangle(int x, int y, int w, int h, Color color);
166
 
167
    public abstract void drawRectangle(int x, int y, int w, int h, Color color, int lineWidth);
168
 
169
    public abstract void drawBarcode(int x, int y, int h, int type, String code, int moduleWidth, int fontSize);
170
 
171
}