Search in sources :

Example 1 with PDExtendedGraphicsState

use of org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState in project com.revolsys.open by revolsys.

the class PdfViewport method setGeometryStyle.

private void setGeometryStyle(final GeometryStyle style) throws IOException {
    String styleName = this.styleNames.get(style);
    if (styleName == null) {
        styleName = "rgStyle" + this.styleId++;
        final PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
        final int lineOpacity = style.getLineOpacity();
        if (lineOpacity != 255) {
            graphicsState.setStrokingAlphaConstant(lineOpacity / 255f);
        }
        final Quantity<Length> lineWidth = style.getLineWidth();
        final Unit<Length> unit = lineWidth.getUnit();
        graphicsState.setLineWidth((float) toDisplayValue(lineWidth));
        final List<Double> lineDashArray = style.getLineDashArray();
        if (lineDashArray != null && !lineDashArray.isEmpty()) {
            int size = lineDashArray.size();
            if (size == 1) {
                size++;
            }
            final float[] dashArray = new float[size];
            for (int i = 0; i < dashArray.length; i++) {
                if (i < lineDashArray.size()) {
                    final Double dashDouble = lineDashArray.get(i);
                    final Quantity<Length> dashMeasure = Quantities.getQuantity(dashDouble, unit);
                    final float dashFloat = (float) toDisplayValue(dashMeasure);
                    dashArray[i] = dashFloat;
                } else {
                    dashArray[i] = dashArray[i - 1];
                }
            }
            final int offset = (int) toDisplayValue(Quantities.getQuantity(style.getLineDashOffset(), unit));
            final COSArray dashCosArray = new COSArray();
            dashCosArray.setFloatArray(dashArray);
            final PDLineDashPattern pattern = new PDLineDashPattern(dashCosArray, offset);
            graphicsState.setLineDashPattern(pattern);
        }
        switch(style.getLineCap()) {
            case BUTT:
                graphicsState.setLineCapStyle(0);
                break;
            case ROUND:
                graphicsState.setLineCapStyle(1);
                break;
            case SQUARE:
                graphicsState.setLineCapStyle(2);
                break;
        }
        switch(style.getLineJoin()) {
            case MITER:
                graphicsState.setLineJoinStyle(0);
                break;
            case ROUND:
                graphicsState.setLineJoinStyle(1);
                break;
            case BEVEL:
                graphicsState.setLineJoinStyle(2);
                break;
        }
        final int polygonFillOpacity = style.getPolygonFillOpacity();
        if (polygonFillOpacity != 255) {
            graphicsState.setNonStrokingAlphaConstant(polygonFillOpacity / 255f);
        }
        final PDResources resources = this.page.findResources();
        Map<String, PDExtendedGraphicsState> graphicsStateDictionary = resources.getGraphicsStates();
        if (graphicsStateDictionary == null) {
            graphicsStateDictionary = new TreeMap<>();
        }
        graphicsStateDictionary.put(styleName, graphicsState);
        resources.setGraphicsStates(graphicsStateDictionary);
        this.styleNames.put(style, styleName);
    }
    this.contentStream.appendRawCommands("/" + styleName + " gs\n");
}
Also used : PDExtendedGraphicsState(org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState) PDResources(org.apache.pdfbox.pdmodel.PDResources) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) COSArray(org.apache.pdfbox.cos.COSArray) Length(javax.measure.quantity.Length) PDLineDashPattern(org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern)

Aggregations

LineString (com.revolsys.geometry.model.LineString)1 Point (com.revolsys.geometry.model.Point)1 Length (javax.measure.quantity.Length)1 COSArray (org.apache.pdfbox.cos.COSArray)1 PDResources (org.apache.pdfbox.pdmodel.PDResources)1 PDExtendedGraphicsState (org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState)1 PDLineDashPattern (org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern)1