woo-j / OkapiBarcode

Open-source barcode encoding program written in Java
http://www.okapibarcode.org.uk
Apache License 2.0
328 stars 97 forks source link

Orientation Adjust #114

Open melloware opened 1 week ago

melloware commented 1 week ago

I am converting from Barcode4J and one thing it had was the ability to adjust the Orientation.

For the Java2d Renderer...

public void establishDimensions(BarcodeDimension dim) {
        super.establishDimensions(dim);
        int orientation = BarcodeDimension.normalizeOrientation(getOrientation());
        double w = dim.getWidthPlusQuiet(orientation);
        double h = dim.getHeightPlusQuiet(orientation);
        this.g2d = (Graphics2D)this.g2d.create();
        switch (orientation) {
        case 90:
            g2d.rotate(-Math.PI / 2);
            g2d.translate(-h, 0);
            break;
        case 180:
            g2d.rotate(-Math.PI);
            g2d.translate(-w, -h);
            break;
        case 270:
            g2d.rotate(-Math.PI * 1.5);
            g2d.translate(0, -w);
            break;
        default:
            //nop
        }
    }

For the SVG Renderer:

 public void establishDimensions(BarcodeDimension dim) {
        super.establishDimensions(dim);
        int orientation = BarcodeDimension.normalizeOrientation(getOrientation());
        Element svg = (Element)doc.getDocumentElement();
        svg.setAttribute("width", addUnit(dim.getWidthPlusQuiet(orientation)));
        svg.setAttribute("height", addUnit(dim.getHeightPlusQuiet(orientation)));
        String w = getDecimalFormat().format(dim.getWidthPlusQuiet(orientation));
        String h = getDecimalFormat().format(dim.getHeightPlusQuiet(orientation));
        svg.setAttribute("viewBox", "0 0 " + w + " " + h);
        String transform;
        switch (orientation) {
        case 90:
            transform = "rotate(-90) translate(-" + h + ")";
            break;
        case 180:
            transform = "rotate(-180) translate(-" + w + " -" + h + ")";
            break;
        case 270:
            transform = "rotate(-270) translate(0 -" + w + ")";
            break;
        default:
            transform = null;
        }
        if (transform != null) {
            detailGroup.setAttribute("transform", transform);
        }
    }

And this method is how it normalized.

public static int normalizeOrientation(int orientation) {
        switch (orientation) {
            case 0:
                return 0;
            case 90:
            case -270:
                return 90;
            case 180:
            case -180:
                return 180;
            case 270:
            case -90:
                return 270;
            default:
                throw new IllegalArgumentException(
                            "Orientation must be 0, 90, 180, 270, -90, -180 or -270");
        }
    }

It would be great to get this in Okapi!

gredler commented 1 week ago

Interesting idea. Not really necessary for Java2D, since the Graphics2D object can be transformed prior to providing it to the Okapi renderer, but it could be useful for SVG and PS (and if we add it there, we might as well keep things consistent and add it to Java2D). I'll have a look in the next few days.

melloware commented 1 week ago

Awesome! For reference this is what I am replacing https://www.primefaces.org/showcase/ui/multimedia/barcode.xhtml

You can see it in the UPC example.