Search in sources :

Example 76 with AffineTransform

use of java.awt.geom.AffineTransform in project poi by apache.

the class XSLFFreeformShape method setPath.

@Override
public int setPath(Path2D.Double path) {
    CTPath2D ctPath = CTPath2D.Factory.newInstance();
    Rectangle2D bounds = path.getBounds2D();
    int x0 = Units.toEMU(bounds.getX());
    int y0 = Units.toEMU(bounds.getY());
    PathIterator it = path.getPathIterator(new AffineTransform());
    int numPoints = 0;
    ctPath.setH(Units.toEMU(bounds.getHeight()));
    ctPath.setW(Units.toEMU(bounds.getWidth()));
    while (!it.isDone()) {
        double[] vals = new double[6];
        int type = it.currentSegment(vals);
        switch(type) {
            case PathIterator.SEG_MOVETO:
                CTAdjPoint2D mv = ctPath.addNewMoveTo().addNewPt();
                mv.setX(Units.toEMU(vals[0]) - x0);
                mv.setY(Units.toEMU(vals[1]) - y0);
                numPoints++;
                break;
            case PathIterator.SEG_LINETO:
                CTAdjPoint2D ln = ctPath.addNewLnTo().addNewPt();
                ln.setX(Units.toEMU(vals[0]) - x0);
                ln.setY(Units.toEMU(vals[1]) - y0);
                numPoints++;
                break;
            case PathIterator.SEG_QUADTO:
                CTPath2DQuadBezierTo qbez = ctPath.addNewQuadBezTo();
                CTAdjPoint2D qp1 = qbez.addNewPt();
                qp1.setX(Units.toEMU(vals[0]) - x0);
                qp1.setY(Units.toEMU(vals[1]) - y0);
                CTAdjPoint2D qp2 = qbez.addNewPt();
                qp2.setX(Units.toEMU(vals[2]) - x0);
                qp2.setY(Units.toEMU(vals[3]) - y0);
                numPoints += 2;
                break;
            case PathIterator.SEG_CUBICTO:
                CTPath2DCubicBezierTo bez = ctPath.addNewCubicBezTo();
                CTAdjPoint2D p1 = bez.addNewPt();
                p1.setX(Units.toEMU(vals[0]) - x0);
                p1.setY(Units.toEMU(vals[1]) - y0);
                CTAdjPoint2D p2 = bez.addNewPt();
                p2.setX(Units.toEMU(vals[2]) - x0);
                p2.setY(Units.toEMU(vals[3]) - y0);
                CTAdjPoint2D p3 = bez.addNewPt();
                p3.setX(Units.toEMU(vals[4]) - x0);
                p3.setY(Units.toEMU(vals[5]) - y0);
                numPoints += 3;
                break;
            case PathIterator.SEG_CLOSE:
                numPoints++;
                ctPath.addNewClose();
                break;
            default:
                throw new IllegalStateException("Unrecognized path segment type: " + type);
        }
        it.next();
    }
    XmlObject xo = getShapeProperties();
    if (!(xo instanceof CTShapeProperties)) {
        return -1;
    }
    ((CTShapeProperties) xo).getCustGeom().getPathLst().setPathArray(new CTPath2D[] { ctPath });
    setAnchor(bounds);
    return numPoints;
}
Also used : CTAdjPoint2D(org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D) CTPath2DCubicBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo) CTPath2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D) CTShapeProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties) PathIterator(java.awt.geom.PathIterator) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) XmlObject(org.apache.xmlbeans.XmlObject) CTPath2DQuadBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DQuadBezierTo)

Example 77 with AffineTransform

use of java.awt.geom.AffineTransform in project intellij-community by JetBrains.

the class DiffLineSeparatorRenderer method paintLine.

private static void paintLine(@NotNull Graphics g, @NotNull int[] xPoints, @NotNull int[] yPoints, int lineHeight, @Nullable EditorColorsScheme scheme) {
    int height = getHeight(lineHeight);
    if (scheme == null)
        scheme = EditorColorsManager.getInstance().getGlobalScheme();
    Graphics2D gg = ((Graphics2D) g);
    AffineTransform oldTransform = gg.getTransform();
    for (int i = 0; i < height; i++) {
        Color color = getTopBorderColor(i, lineHeight, scheme);
        if (color == null)
            color = getBottomBorderColor(i, lineHeight, scheme);
        if (color == null)
            color = getBackgroundColor(scheme);
        gg.setColor(color);
        gg.drawPolyline(xPoints, yPoints, xPoints.length);
        gg.translate(0, 1);
    }
    gg.setTransform(oldTransform);
}
Also used : AffineTransform(java.awt.geom.AffineTransform)

Example 78 with AffineTransform

use of java.awt.geom.AffineTransform in project poi by apache.

the class HwmfPicture method draw.

public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
    AffineTransform at = ctx.getTransform();
    try {
        Rectangle2D wmfBounds = getBounds();
        // scale output bounds to image bounds
        ctx.translate(graphicsBounds.getX(), graphicsBounds.getY());
        ctx.scale(graphicsBounds.getWidth() / wmfBounds.getWidth(), graphicsBounds.getHeight() / wmfBounds.getHeight());
        HwmfGraphics g = new HwmfGraphics(ctx, wmfBounds);
        for (HwmfRecord r : records) {
            r.draw(g);
        }
    } finally {
        ctx.setTransform(at);
    }
}
Also used : HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) HwmfGraphics(org.apache.poi.hwmf.draw.HwmfGraphics)

Example 79 with AffineTransform

use of java.awt.geom.AffineTransform in project poi by apache.

the class HwmfGraphics method drawString.

public void drawString(byte[] text, Rectangle2D bounds, int[] dx) {
    HwmfFont font = prop.getFont();
    if (font == null || text == null || text.length == 0) {
        return;
    }
    double fontH = getFontHeight(font);
    // TODO: another approx. ...
    double fontW = fontH / 1.8;
    int len = text.length;
    Charset charset = (font.getCharSet().getCharset() == null) ? DEFAULT_CHARSET : font.getCharSet().getCharset();
    String textString = new String(text, charset);
    AttributedString as = new AttributedString(textString);
    if (dx == null || dx.length == 0) {
        addAttributes(as, font);
    } else {
        int[] dxNormed = dx;
        //dxNormed[1] = 14 textString.get(1) = U+30ED
        if (textString.length() != text.length) {
            int codePoints = textString.codePointCount(0, textString.length());
            dxNormed = new int[codePoints];
            int dxPosition = 0;
            for (int offset = 0; offset < textString.length(); ) {
                dxNormed[offset] = dx[dxPosition];
                int[] chars = new int[1];
                int cp = textString.codePointAt(offset);
                chars[0] = cp;
                //now figure out how many bytes it takes to encode that
                //code point in the charset
                int byteLength = new String(chars, 0, chars.length).getBytes(charset).length;
                dxPosition += byteLength;
                offset += Character.charCount(cp);
            }
        }
        for (int i = 0; i < dxNormed.length; i++) {
            addAttributes(as, font);
            // therefore we need to add the additional/suffix width to the next char
            if (i < dxNormed.length - 1) {
                as.addAttribute(TextAttribute.TRACKING, (dxNormed[i] - fontW) / fontH, i + 1, i + 2);
            }
        }
    }
    double angle = Math.toRadians(-font.getEscapement() / 10.);
    final AffineTransform at = graphicsCtx.getTransform();
    try {
        graphicsCtx.translate(bounds.getX(), bounds.getY() + fontH);
        graphicsCtx.rotate(angle);
        if (prop.getBkMode() == HwmfBkMode.OPAQUE) {
            // TODO: validate bounds
            graphicsCtx.setBackground(prop.getBackgroundColor().getColor());
            graphicsCtx.fill(new Rectangle2D.Double(0, 0, bounds.getWidth(), bounds.getHeight()));
        }
        graphicsCtx.setColor(prop.getTextColor().getColor());
        // (float)bounds.getX(), (float)bounds.getY());
        graphicsCtx.drawString(as.getIterator(), 0, 0);
    } finally {
        graphicsCtx.setTransform(at);
    }
}
Also used : AttributedString(java.text.AttributedString) Rectangle2D(java.awt.geom.Rectangle2D) Charset(java.nio.charset.Charset) AffineTransform(java.awt.geom.AffineTransform) AttributedString(java.text.AttributedString) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint)

Example 80 with AffineTransform

use of java.awt.geom.AffineTransform in project limelight by slagyr.

the class ImagePanelLayoutTest method hasConstrainedProportionsWhenHeightIsNotAuto.

@Test
public void hasConstrainedProportionsWhenHeightIsNotAuto() throws Exception {
    panel.setFilename(TestUtil.DATA_DIR + "/star.gif");
    parent.style.setHeight("100");
    parent.childConsumableBounds = new Box(0, 0, 200, 100);
    Layouts.on(parent, parent.getDefaultLayout());
    AffineTransform tranform = panel.getTransform();
    assertEquals(0.5, tranform.getScaleX(), 0.001);
    assertEquals(0.5, tranform.getScaleY(), 0.001);
}
Also used : AffineTransform(java.awt.geom.AffineTransform) Box(limelight.util.Box) Test(org.junit.Test)

Aggregations

AffineTransform (java.awt.geom.AffineTransform)370 BufferedImage (java.awt.image.BufferedImage)60 Graphics2D (java.awt.Graphics2D)54 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)42 Rectangle2D (java.awt.geom.Rectangle2D)40 Point2D (java.awt.geom.Point2D)28 Shape (java.awt.Shape)24 Font (java.awt.Font)23 Paint (java.awt.Paint)23 GcSnapshot (com.android.layoutlib.bridge.impl.GcSnapshot)20 ArrayList (java.util.ArrayList)18 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)17 Rectangle (java.awt.Rectangle)16 PathIterator (java.awt.geom.PathIterator)16 Color (java.awt.Color)15 Point (java.awt.Point)15 FontRenderContext (java.awt.font.FontRenderContext)15 Area (java.awt.geom.Area)14 GeneralPath (java.awt.geom.GeneralPath)14 AffineTransformOp (java.awt.image.AffineTransformOp)13