Example usage for java.awt.geom GeneralPath transform

List of usage examples for java.awt.geom GeneralPath transform

Introduction

In this page you can find the example usage for java.awt.geom GeneralPath transform.

Prototype

public abstract void transform(AffineTransform at);

Source Link

Document

Transforms the geometry of this path using the specified AffineTransform .

Usage

From source file:org.apache.pdfbox.pdfviewer.font.CFFGlyph2D.java

/**
 * Constructor./*from  w  w  w. ja  v a 2 s .com*/
 * 
 */
public CFFGlyph2D(CFFFont cffFont, Encoding encoding) {
    fontname = cffFont.getName();
    Map<String, Integer> nameToCode = encoding != null ? encoding.getNameToCodeMap() : null;
    Collection<CFFFont.Mapping> mappings = cffFont.getMappings();
    Map<Integer, String> codeToNameMap = new LinkedHashMap<Integer, String>();
    for (CFFFont.Mapping mapping : mappings) {
        codeToNameMap.put(mapping.getCode(), mapping.getName());
    }

    CharStringRenderer renderer = cffFont.createRenderer();
    int glyphId = 0;
    for (CFFFont.Mapping mapping : mappings) {
        GeneralPath glyph = null;
        try {
            glyph = renderer.render(mapping.toType1Sequence());
        } catch (IOException exception) {
            LOG.error("CFF glyph rendering fails!", exception);
        }
        if (glyph != null) {
            AffineTransform atPath = AffineTransform.getScaleInstance(scale, scale);
            glyph.transform(atPath);
            glyphs.put(glyphId, transformGlyph(glyph));
            int code = mapping.getSID();
            String name = mapping.getName();
            if (nameToCode != null && nameToCode.containsKey(name)) {
                code = nameToCode.get(name);
            }
            codeToGlyph.put(code, glyphId);
            glyphId++;
        }
    }
}

From source file:org.apache.pdfbox.pdfviewer.font.TTFGlyph2D.java

/**
 * Returns the path describing the glyph for the given glyphId.
 *
 * @param glyphId the glyphId/* w ww .  j a  v a 2  s  .  c om*/
 *
 * @return the GeneralPath for the given glyphId
 */
public GeneralPath getPathForGlyphId(int glyphId) {
    GeneralPath glyphPath = null;
    if (glyphs.containsKey(glyphId)) {
        glyphPath = glyphs.get(glyphId);
    } else {
        GlyphData[] glyphData = font.getGlyph().getGlyphs();
        if (glyphId < glyphData.length && glyphData[glyphId] != null) {
            GlyphData glyph = glyphData[glyphId];
            GlyphDescription gd = glyph.getDescription();
            Point[] points = describe(gd);
            glyphPath = calculatePath(points);
            if (hasScaling) {
                AffineTransform atScale = AffineTransform.getScaleInstance(scale, scale);
                glyphPath.transform(atScale);
            }
            glyphs.put(glyphId, glyphPath);
        } else {
            LOG.debug(name + ": Glyph not found:" + glyphId);
        }
    }
    return glyphPath != null ? (GeneralPath) glyphPath.clone() : null;
}

From source file:org.apache.pdfbox.rendering.TTFGlyph2D.java

/**
 * Returns the path describing the glyph for the given glyphId.
 *
 * @param gid the GID//from   w ww  . ja v a  2 s  .  co m
 * @param code the character code
 *
 * @return the GeneralPath for the given glyphId
 */
public GeneralPath getPathForGID(int gid, int code) throws IOException {
    GeneralPath glyphPath;
    if (glyphs.containsKey(gid)) {
        glyphPath = glyphs.get(gid);
    } else {
        if (gid == 0 || gid >= ttf.getMaximumProfile().getNumGlyphs()) {
            if (isCIDFont) {
                int cid = ((PDType0Font) font).codeToCID(code);
                String cidHex = String.format("%04x", cid);
                LOG.warn("No glyph for " + code + " (CID " + cidHex + ") in font " + font.getName());
            } else {
                LOG.warn("No glyph for " + code + " in font " + font.getName());
            }
        }

        GeneralPath glyph = vectorFont.getPath(code);

        // Acrobat only draws GID 0 for embedded or "Standard 14" fonts, see PDFBOX-2372
        if (gid == 0 && !font.isEmbedded() && !font.isStandard14()) {
            glyph = null;
        }

        if (glyph == null) {
            // empty glyph (e.g. space, newline)
            glyphPath = new GeneralPath();
            glyphs.put(gid, glyphPath);
        } else {
            glyphPath = glyph;
            if (hasScaling) {
                AffineTransform atScale = AffineTransform.getScaleInstance(scale, scale);
                glyphPath.transform(atScale);
            }
            glyphs.put(gid, glyphPath);
        }
    }
    return glyphPath != null ? (GeneralPath) glyphPath.clone() : null; // todo: expensive
}

From source file:se.ngm.ditaaeps.EpsRenderer.java

public static void renderToEps(Diagram diagram, PrintWriter out, RenderingOptions options) {
    //RenderedImage renderedImage = image;
    EpsGraphics2D g2 = new EpsGraphics2D(out,
            new Rectangle2D.Double(0, -diagram.getHeight(), diagram.getWidth(), diagram.getHeight()));

    g2.scale(1, -1); // g2 origo is top-left, eps is bottom-left

    Object antialiasSetting = antialiasSetting = RenderingHints.VALUE_ANTIALIAS_OFF;
    if (options.performAntialias())
        antialiasSetting = RenderingHints.VALUE_ANTIALIAS_ON;

    //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);

    g2.setColor(Color.white);//from  w  w  w.j av a 2 s  . c  o  m
    //TODO: find out why the next line does not work
    //g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10);
    /*for(int y = 0; y < diagram.getHeight(); y ++)
      g2.drawLine(0, y, diagram.getWidth(), y);*/

    g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

    ArrayList shapes = diagram.getAllDiagramShapes();

    if (DEBUG)
        System.out.println("Rendering " + shapes.size() + " shapes (groups flattened)");

    Iterator shapesIt;
    if (options.dropShadows()) {
        //render shadows
        shapesIt = shapes.iterator();
        while (shapesIt.hasNext()) {
            DiagramShape shape = (DiagramShape) shapesIt.next();

            if (shape.getPoints().isEmpty())
                continue;

            //GeneralPath path = shape.makeIntoPath();
            GeneralPath path;
            path = shape.makeIntoRenderPath(diagram);

            float offset = diagram.getMinimumOfCellDimension() / 3.333f;

            if (path != null && shape.dropsShadow()) {
                GeneralPath shadow = new GeneralPath(path);
                AffineTransform translate = new AffineTransform();
                translate.setToTranslation(offset, offset);
                shadow.transform(translate);
                g2.setColor(new Color(150, 150, 150));
                g2.fill(shadow);

            }
        }

        //blur shadows

        //            if(true) {
        //                int blurRadius = 6;
        //                int blurRadius2 = blurRadius * blurRadius;
        //                float blurRadius2F = blurRadius2;
        //                float weight = 1.0f / blurRadius2F;
        //                float[] elements = new float[blurRadius2];
        //                for (int k = 0; k < blurRadius2; k++)
        //                    elements[k] = weight;
        //                Kernel myKernel = new Kernel(blurRadius, blurRadius, elements);
        //
        //                //if EDGE_NO_OP is not selected, EDGE_ZERO_FILL is the default which creates a black border
        //                ConvolveOp simpleBlur =
        //                        new ConvolveOp(myKernel, ConvolveOp.EDGE_NO_OP, null);
        //                //BufferedImage destination = new BufferedImage(image.getWidth()+blurRadius, image.getHeight()+blurRadius, image.getType());
        //                BufferedImage destination =
        //                        new BufferedImage(
        //                        image.getWidth(),
        //                        image.getHeight(),
        //                        image.getType());
        //                simpleBlur.filter(image, destination);
        //                //destination = destination.getSubimage(blurRadius/2, blurRadius/2, image.getWidth(), image.getHeight());
        //                g2 = destination.createGraphics();
        //                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
        //                renderedImage = destination;
        //            }
    }

    //fill and stroke

    float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2;
    //Stroke normalStroke = g2.getStroke();

    float strokeWeight = diagram.getMinimumOfCellDimension() / 10;

    Stroke normalStroke = new BasicStroke(strokeWeight,
            //10,
            BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

    Stroke dashStroke = new BasicStroke(strokeWeight, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0,
            new float[] { dashInterval }, 0);

    //find storage shapes
    ArrayList storageShapes = new ArrayList();
    shapesIt = shapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        if (shape.getType() == DiagramShape.TYPE_STORAGE) {
            storageShapes.add(shape);
            continue;
        }
    }

    //render storage shapes
    //special case since they are '3d' and should be
    //rendered bottom to top
    //TODO: known bug: if a storage object is within a bigger normal box, it will be overwritten in the main drawing loop
    //(BUT this is not possible since tags are applied to all shapes overlaping shapes)

    Collections.sort(storageShapes, new Shape3DOrderingComparator());

    g2.setStroke(normalStroke);
    shapesIt = storageShapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        if (!shape.isStrokeDashed()) {
            if (shape.getFillColor() != null)
                g2.setColor(shape.getFillColor());
            else
                g2.setColor(Color.white);
            g2.fill(path);
        }

        if (shape.isStrokeDashed())
            g2.setStroke(dashStroke);
        else
            g2.setStroke(normalStroke);
        g2.setColor(shape.getStrokeColor());
        g2.draw(path);
    }

    //render the rest of the shapes
    ArrayList pointMarkers = new ArrayList();
    shapesIt = shapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        if (shape.getType() == DiagramShape.TYPE_POINT_MARKER) {
            pointMarkers.add(shape);
            continue;
        }
        if (shape.getType() == DiagramShape.TYPE_STORAGE) {
            continue;
        }

        if (shape.getPoints().isEmpty())
            continue;

        int size = shape.getPoints().size();

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        if (path != null && shape.isClosed() && !shape.isStrokeDashed()) {
            if (shape.getFillColor() != null)
                g2.setColor(shape.getFillColor());
            else
                g2.setColor(Color.white);
            g2.fill(path);
        }
        if (shape.getType() != DiagramShape.TYPE_ARROWHEAD) {
            g2.setColor(shape.getStrokeColor());
            if (shape.isStrokeDashed())
                g2.setStroke(dashStroke);
            else
                g2.setStroke(normalStroke);
            g2.draw(path);
        }
    }

    //render point markers

    g2.setStroke(normalStroke);
    shapesIt = pointMarkers.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        //if(shape.getType() != DiagramShape.TYPE_POINT_MARKER) continue;

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        g2.setColor(Color.white);
        g2.fill(path);
        g2.setColor(shape.getStrokeColor());
        g2.draw(path);
    }

    //handle text
    //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    Iterator textIt = diagram.getTextObjects().iterator();
    while (textIt.hasNext()) {
        DiagramText text = (DiagramText) textIt.next();
        g2.setColor(text.getColor());
        g2.setFont(text.getFont());
        g2.drawString(text.getText(), text.getXPos(), text.getYPos());
    }

    if (options.renderDebugLines() || DEBUG) {
        Stroke debugStroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        g2.setStroke(debugStroke);
        g2.setColor(new Color(170, 170, 170));
        g2.setXORMode(Color.white);
        for (int x = 0; x < diagram.getWidth(); x += diagram.getCellWidth())
            g2.drawLine(x, 0, x, diagram.getHeight());
        for (int y = 0; y < diagram.getHeight(); y += diagram.getCellHeight())
            g2.drawLine(0, y, diagram.getWidth(), y);
    }

    g2.dispose();
}