OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

package org.openconcerto.modules.label.graphicspl;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import uk.org.okapibarcode.backend.Code128;
import uk.org.okapibarcode.backend.DataMatrix;
import uk.org.okapibarcode.backend.Ean;
import uk.org.okapibarcode.backend.QrCode;
import uk.org.okapibarcode.backend.Symbol;
import uk.org.okapibarcode.output.Java2DRenderer;

public class Graphics2DRenderer extends GPLRenderer {

    private final Graphics2D g;

    public Graphics2DRenderer(Graphics2D g, float ratio) {
        super(ratio);
        this.g = g;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    }

    @Override
    public void drawText(int x, int y, String text, int align, String fontName, int fontSize, Color color, int maxWidth, boolean wrap) {
        final Font font = new Font(fontName, Font.PLAIN, fontSize);
        g.setFont(font.deriveFont(fontSize));
        g.setColor(color);
        y += g.getFontMetrics().getAscent() - g.getFontMetrics().getDescent();
        if (align == ALIGN_RIGHT) {
            int w = (int) g.getFontMetrics().getStringBounds(text, g).getWidth();
            g.drawString(text, x - w, y);
        } else if (align == ALIGN_CENTER) {
            int w = (int) (g.getFontMetrics().getStringBounds(text, g).getWidth() / 2D);
            g.drawString(text, x - w, y);
        } else {
            g.drawString(text, x, y);
        }
    }

    @Override
    public void drawImage(int x, int y, int w, int h, BufferedImage img) {
        g.drawImage(img, x, y, x + w, y + h, 0, 0, img.getWidth(), img.getHeight(), null);
    }

    @Override
    public void fillRectangle(int x, int y, int w, int h, Color color) {
        g.setColor(color);
        g.fillRect(x, y, w, h);
    }

    @Override
    public void drawRectangle(int x, int y, int w, int h, Color color, int lineWidth) {
        Stroke s = g.getStroke();
        g.setColor(color);
        if (lineWidth != 1) {
            g.setStroke(new BasicStroke(lineWidth));
        }
        g.drawRect(x, y, w, h);
        g.setStroke(s);
    }

    @Override
    public void drawBarcode(int x, int y, int h, int type, String code, int moduleWidth, int fontSize) {

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        Symbol symbol = null;
        if (type == BARCODE_EAN8) {
            symbol = new Ean();
            ((Ean) symbol).setMode(Ean.Mode.EAN8);
            symbol.setBarHeight(h);
        } else if (type == BARCODE_EAN13) {
            symbol = new Ean();
            ((Ean) symbol).setMode(Ean.Mode.EAN13);
            symbol.setBarHeight(h);
        } else if (type == BARCODE_CODE128) {
            symbol = new Code128();
            symbol.setDataType(Symbol.DataType.GS1);
            symbol.setBarHeight(h);
        } else if (type == BARCODE_CODE128_GS1) {
            symbol = new Code128();
            symbol.setBarHeight(h);
        } else if (type == BARCODE_DATAMATRIX) {
            symbol = new DataMatrix();
        } else if (type == BARCODE_QRCODE) {
            symbol = new QrCode();
        }

        if (symbol == null) {
            return;
        }
        symbol.setModuleWidth(moduleWidth);
        symbol.setFontSize(fontSize);
        symbol.setContent(code.trim());

        AffineTransform aT = g.getTransform();

        g.setTransform(AffineTransform.getTranslateInstance(x, y));

        Java2DRenderer renderer = new Java2DRenderer(g, 1, Color.WHITE, Color.BLACK);
        renderer.render(symbol);

        g.setTransform(aT);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    }

}