Java Code Examples for java.awt.geom.AffineTransform#getType()

The following examples show how to use java.awt.geom.AffineTransform#getType() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void checkBug4418285() {
    AffineTransform id =
        new AffineTransform ();
    AffineTransform translate0 =
        AffineTransform.getTranslateInstance (0, 0);
    if (id.isIdentity() != translate0.isIdentity() ||
        id.getType() != translate0.getType())
    {
        numerrors++;
        if (verbose) {
            System.err.println("id=        " + id         +
                               ", isIdentity()=" +
                               id.isIdentity());
            System.err.println("translate0=" + translate0 +
                               ", isIdentity()=" +
                               translate0.isIdentity());
            System.err.println("equals="     + id.equals (translate0));
            System.err.println();
        }
    }
}
 
Example 2
@Override
protected void deviceFillRect(int x, int y, int width, int height,
                              Color color) {
    /*
     * Transform to device coordinates
     */
    AffineTransform deviceTransform = getTransform();

    /* check if rotated or sheared */
    int transformType = deviceTransform.getType();
    boolean usePath =  ((transformType &
                           (AffineTransform.TYPE_GENERAL_ROTATION |
                            AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
    if (usePath) {
        fill(new Rectangle2D.Float(x, y, width, height));
        return;
    }

    Point2D.Float tlc_pos = new Point2D.Float(x, y);
    deviceTransform.transform(tlc_pos, tlc_pos);

    Point2D.Float brc_pos = new Point2D.Float(x+width, y+height);
    deviceTransform.transform(brc_pos, brc_pos);

    float deviceWidth = (float) (brc_pos.getX() - tlc_pos.getX());
    float deviceHeight = (float)(brc_pos.getY() - tlc_pos.getY());

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
    wPrinterJob.fillRect((float)tlc_pos.getX(), (float)tlc_pos.getY(),
                         deviceWidth, deviceHeight, color);
}
 
Example 3
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 4
/**
 * Return true if drawing <code>img</code> will
 * invoke a Java2D bug (#4258675). The bug in question
 * occurs when a draw image call with a background color
 * parameter tries to render a sheared
 * or rotated image. The portions of the bounding
 * rectangle not covered by the sheared image
 * are incorrectly drawn with the background color.
 */
private boolean needToCopyBgColorImage(Image img) {

    boolean needToCopy;

    AffineTransform transform = getTransform();

    return (transform.getType()
            & (AffineTransform.TYPE_GENERAL_ROTATION
               | AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0;
}
 
Example 5
protected static Shape transformShape(AffineTransform tx, Shape clip) {
    if (clip == null) {
        return null;
    }

    if (clip instanceof Rectangle2D &&
        (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
    {
        Rectangle2D rect = (Rectangle2D) clip;
        double matrix[] = new double[4];
        matrix[0] = rect.getX();
        matrix[1] = rect.getY();
        matrix[2] = matrix[0] + rect.getWidth();
        matrix[3] = matrix[1] + rect.getHeight();
        tx.transform(matrix, 0, matrix, 0, 2);
        fixRectangleOrientation(matrix, rect);
        return new Rectangle2D.Double(matrix[0], matrix[1],
                                      matrix[2] - matrix[0],
                                      matrix[3] - matrix[1]);
    }

    if (tx.isIdentity()) {
        return cloneShape(clip);
    }

    return tx.createTransformedShape(clip);
}
 
Example 6
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 7
@Override
protected void deviceFillRect(int x, int y, int width, int height,
                              Color color) {
    /*
     * Transform to device coordinates
     */
    AffineTransform deviceTransform = getTransform();

    /* check if rotated or sheared */
    int transformType = deviceTransform.getType();
    boolean usePath =  ((transformType &
                           (AffineTransform.TYPE_GENERAL_ROTATION |
                            AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
    if (usePath) {
        fill(new Rectangle2D.Float(x, y, width, height));
        return;
    }

    Point2D.Float tlc_pos = new Point2D.Float(x, y);
    deviceTransform.transform(tlc_pos, tlc_pos);

    Point2D.Float brc_pos = new Point2D.Float(x+width, y+height);
    deviceTransform.transform(brc_pos, brc_pos);

    float deviceWidth = (float) (brc_pos.getX() - tlc_pos.getX());
    float deviceHeight = (float)(brc_pos.getY() - tlc_pos.getY());

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
    wPrinterJob.fillRect((float)tlc_pos.getX(), (float)tlc_pos.getY(),
                         deviceWidth, deviceHeight, color);
}
 
Example 8
/**
 * Return true if drawing <code>img</code> will
 * invoke a Java2D bug (#4258675). The bug in question
 * occurs when a draw image call with a background color
 * parameter tries to render a sheared
 * or rotated image. The portions of the bounding
 * rectangle not covered by the sheared image
 * are incorrectly drawn with the background color.
 */
private boolean needToCopyBgColorImage(Image img) {

    boolean needToCopy;

    AffineTransform transform = getTransform();

    return (transform.getType()
            & (AffineTransform.TYPE_GENERAL_ROTATION
               | AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0;
}
 
Example 9
@Override
protected void deviceFillRect(int x, int y, int width, int height,
                              Color color) {
    /*
     * Transform to device coordinates
     */
    AffineTransform deviceTransform = getTransform();

    /* check if rotated or sheared */
    int transformType = deviceTransform.getType();
    boolean usePath =  ((transformType &
                           (AffineTransform.TYPE_GENERAL_ROTATION |
                            AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
    if (usePath) {
        fill(new Rectangle2D.Float(x, y, width, height));
        return;
    }

    Point2D.Float tlc_pos = new Point2D.Float(x, y);
    deviceTransform.transform(tlc_pos, tlc_pos);

    Point2D.Float brc_pos = new Point2D.Float(x+width, y+height);
    deviceTransform.transform(brc_pos, brc_pos);

    float deviceWidth = (float) (brc_pos.getX() - tlc_pos.getX());
    float deviceHeight = (float)(brc_pos.getY() - tlc_pos.getY());

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
    wPrinterJob.fillRect((float)tlc_pos.getX(), (float)tlc_pos.getY(),
                         deviceWidth, deviceHeight, color);
}
 
Example 10
private float userSpaceLineWidth(AffineTransform at, float lw) {

        double widthScale;

        if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM |
                            AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
            widthScale = Math.sqrt(at.getDeterminant());
        } else {
            /* First calculate the "maximum scale" of this transform. */
            double A = at.getScaleX();       // m00
            double C = at.getShearX();       // m01
            double B = at.getShearY();       // m10
            double D = at.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */

            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2*(A*C + B*D);      // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */

            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
            /* sqrt omitted, compare to squared limits below. */
            double widthsquared = ((EA + EC + hypot)/2.0);

            widthScale = Math.sqrt(widthsquared);
        }

        return (float) (lw / widthScale);
    }
 
Example 11
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}
 
Example 12
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    if (str.length() == 0) {
        return;
    }

    /* If the Font has layout attributes we need to delegate to TextLayout.
     * TextLayout renders text as GlyphVectors. We try to print those
     * using printer fonts - ie using Postscript text operators so
     * we may be reinvoked. In that case the "!printingGlyphVector" test
     * prevents us recursing and instead sends us into the body of the
     * method where we can safely ignore layout attributes as those
     * are already handled by TextLayout.
     */
    if (font.hasLayoutAttributes() && !printingGlyphVector) {
        TextLayout layout = new TextLayout(str, font, frc);
        layout.draw(this, x, y);
        return;
    }

    Font oldFont = getFont();
    if (!oldFont.equals(font)) {
        setFont(font);
    } else {
        oldFont = null;
    }

    boolean drawnWithPS = false;

    float translateX = 0f, translateY = 0f;
    boolean fontisTransformed = getFont().isTransformed();

    if (fontisTransformed) {
        AffineTransform fontTx = getFont().getTransform();
        int transformType = fontTx.getType();
        /* TYPE_TRANSLATION is a flag bit but we can do "==" here
         * because we want to detect when its just that bit set and
         *
         */
        if (transformType == AffineTransform.TYPE_TRANSLATION) {
            translateX = (float)(fontTx.getTranslateX());
            translateY = (float)(fontTx.getTranslateY());
            if (Math.abs(translateX) < 0.00001) translateX = 0f;
            if (Math.abs(translateY) < 0.00001) translateY = 0f;
            fontisTransformed = false;
        }
    }

    boolean directToPS = !fontisTransformed;

    if (!PSPrinterJob.shapeTextProp && directToPS) {

        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
        if (psPrinterJob.setFont(getFont())) {

            /* Set the text color.
             * We should not be in this shape printing path
             * if the application is drawing with non-solid
             * colors. We should be in the raster path. Because
             * we are here in the shape path, the cast of the
             * paint to a Color should be fine.
             */
            try {
                psPrinterJob.setColor((Color)getPaint());
            } catch (ClassCastException e) {
                if (oldFont != null) {
                    setFont(oldFont);
                }
                throw new IllegalArgumentException(
                                            "Expected a Color instance");
            }

            psPrinterJob.setTransform(getTransform());
            psPrinterJob.setClip(getClip());

            drawnWithPS = psPrinterJob.textOut(this, str,
                                               x+translateX, y+translateY,
                                               font, frc, w);
        }
    }

    /* The text could not be converted directly to PS text
     * calls so decompose the text into a shape.
     */
    if (drawnWithPS == false) {
        if (oldFont != null) {
            setFont(oldFont);
            oldFont = null;
        }
        super.drawString(str, x, y, font, frc, w);
    }

    if (oldFont != null) {
        setFont(oldFont);
    }
}
 
Example 13
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}
 
Example 14
Source Project: netbeans   File: VectorIcon.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public final void paintIcon(Component c, Graphics g0, int x, int y) {
    final Graphics2D g2 = createGraphicsWithRenderingHintsConfigured(g0);
    try {
        // Make sure the subclass can't paint outside its stated dimensions.
        g2.clipRect(x, y, getIconWidth(), getIconHeight());
        g2.translate(x, y);
        /**
         * On HiDPI monitors, the Graphics object will have a default transform that maps
         * logical pixels, like those you'd pass to Graphics.drawLine, to a higher number of
         * device pixels on the screen. For instance, painting a line 10 pixels long on the
         * current Graphics object would actually produce a line 20 device pixels long on a
         * MacOS retina screen, which has a DPI scaling factor of 2.0. On Windows 10, many
         * different scaling factors may be encountered, including non-integral ones such as
         * 1.5. Detect the scaling factor here so we can use it to inform the drawing routines.
         */
        final double scaling;
        final AffineTransform tx = g2.getTransform();
        int txType = tx.getType();
        if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
            txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
        {
            scaling = tx.getScaleX();
        } else {
            // Unrecognized transform type. Don't do any custom scaling handling.
            paintIcon(c, g2, getIconWidth(), getIconHeight(), 1.0);
            return;
        }
        /* When using a non-integral scaling factor, such as 175%, preceding Swing components
        often end up being a non-integral number of device pixels tall or wide. This will cause
        our initial position to be "off the grid" with respect to device pixels, causing blurry
        graphics even if we subsequently take care to use only integral numbers of device pixels
        during painting. Fix this here by consuming a little bit of the top and left of the
        icon's dimensions to offset any error. */
        // The initial position, in device pixels.
        final double previousDevicePosX = tx.getTranslateX();
        final double previousDevicePosY = tx.getTranslateY();
        /* The new, aligned position, after a small portion of the icon's dimensions may have
        been consumed to correct it. */
        final double alignedDevicePosX = Math.ceil(previousDevicePosX);
        final double alignedDevicePosY = Math.ceil(previousDevicePosY);
        // Use the aligned position.
        g2.setTransform(new AffineTransform(
            1, 0, 0, 1, alignedDevicePosX, alignedDevicePosY));
        /* The portion of the icon's dimensions that was consumed to correct any initial
        translation misalignment, in device pixels. May be zero. */
        final double transDeviceAdjX = alignedDevicePosX - previousDevicePosX;
        final double transDeviceAdjY = alignedDevicePosY - previousDevicePosY;
        /* Now calculate the dimensions available for painting, also aligned to an integral
        number of device pixels. */
        final int deviceWidth  = (int) Math.floor(getIconWidth()  * scaling - transDeviceAdjX);
        final int deviceHeight = (int) Math.floor(getIconHeight() * scaling - transDeviceAdjY);
        paintIcon(c, g2, deviceWidth, deviceHeight, scaling);
    } finally {
        g2.dispose();
    }
}
 
Example 15
private double userSpaceLineWidth(AffineTransform at, double lw) {

        double widthScale;

        if (at == null) {
            widthScale = 1.0d;
        } else if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM  |
                                    AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
            widthScale = Math.sqrt(at.getDeterminant());
        } else {
            // First calculate the "maximum scale" of this transform.
            double A = at.getScaleX();       // m00
            double C = at.getShearX();       // m01
            double B = at.getShearY();       // m10
            double D = at.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */

            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2.0d * (A*C + B*D); // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */

            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
            // sqrt omitted, compare to squared limits below.
            double widthsquared = ((EA + EC + hypot) / 2.0d);

            widthScale = Math.sqrt(widthsquared);
        }

        return (lw / widthScale);
    }
 
Example 16
/**
* Draw the bounding rectangle using transformed coordinates.
*/
@Override
protected void deviceFrameRect(int x, int y, int width, int height,
                                Color color) {

   AffineTransform deviceTransform = getTransform();

   /* check if rotated or sheared */
   int transformType = deviceTransform.getType();
   boolean usePath = ((transformType &
                      (AffineTransform.TYPE_GENERAL_ROTATION |
                       AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);

   if (usePath) {
       draw(new Rectangle2D.Float(x, y, width, height));
       return;
   }

   Stroke stroke = getStroke();

   if (stroke instanceof BasicStroke) {
       BasicStroke lineStroke = (BasicStroke) stroke;

       int endCap = lineStroke.getEndCap();
       int lineJoin = lineStroke.getLineJoin();


       /* check for default style and try to optimize it by
        * calling the frameRect native function instead of using paths.
        */
       if ((endCap == BasicStroke.CAP_SQUARE) &&
           (lineJoin == BasicStroke.JOIN_MITER) &&
           (lineStroke.getMiterLimit() ==10.0f)) {

           float lineWidth = lineStroke.getLineWidth();
           Point2D.Float penSize = new Point2D.Float(lineWidth,
                                                     lineWidth);

           deviceTransform.deltaTransform(penSize, penSize);
           float deviceLineWidth = Math.min(Math.abs(penSize.x),
                                            Math.abs(penSize.y));

           /* transform upper left coordinate */
           Point2D.Float ul_pos = new Point2D.Float(x, y);
           deviceTransform.transform(ul_pos, ul_pos);

           /* transform lower right coordinate */
           Point2D.Float lr_pos = new Point2D.Float(x + width,
                                                    y + height);
           deviceTransform.transform(lr_pos, lr_pos);

           float w = (float) (lr_pos.getX() - ul_pos.getX());
           float h = (float)(lr_pos.getY() - ul_pos.getY());

           WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

           /* use selectStylePen, if supported */
           if (wPrinterJob.selectStylePen(endCap, lineJoin,
                                      deviceLineWidth, color) == true)  {
               wPrinterJob.frameRect((float)ul_pos.getX(),
                                     (float)ul_pos.getY(), w, h);
           }
           /* not supported, must be a Win 9x */
           else {

               double lowerRes = Math.min(wPrinterJob.getXRes(),
                                          wPrinterJob.getYRes());

               if ((deviceLineWidth/lowerRes) < MAX_THINLINE_INCHES) {
                   /* use the default pen styles for thin pens. */
                   wPrinterJob.selectPen(deviceLineWidth, color);
                   wPrinterJob.frameRect((float)ul_pos.getX(),
                                         (float)ul_pos.getY(), w, h);
               }
               else {
                   draw(new Rectangle2D.Float(x, y, width, height));
               }
           }
       }
       else {
           draw(new Rectangle2D.Float(x, y, width, height));
       }
   }
}
 
Example 17
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    if (str.length() == 0) {
        return;
    }

    /* If the Font has layout attributes we need to delegate to TextLayout.
     * TextLayout renders text as GlyphVectors. We try to print those
     * using printer fonts - ie using Postscript text operators so
     * we may be reinvoked. In that case the "!printingGlyphVector" test
     * prevents us recursing and instead sends us into the body of the
     * method where we can safely ignore layout attributes as those
     * are already handled by TextLayout.
     */
    if (font.hasLayoutAttributes() && !printingGlyphVector) {
        TextLayout layout = new TextLayout(str, font, frc);
        layout.draw(this, x, y);
        return;
    }

    Font oldFont = getFont();
    if (!oldFont.equals(font)) {
        setFont(font);
    } else {
        oldFont = null;
    }

    boolean drawnWithPS = false;

    float translateX = 0f, translateY = 0f;
    boolean fontisTransformed = getFont().isTransformed();

    if (fontisTransformed) {
        AffineTransform fontTx = getFont().getTransform();
        int transformType = fontTx.getType();
        /* TYPE_TRANSLATION is a flag bit but we can do "==" here
         * because we want to detect when its just that bit set and
         *
         */
        if (transformType == AffineTransform.TYPE_TRANSLATION) {
            translateX = (float)(fontTx.getTranslateX());
            translateY = (float)(fontTx.getTranslateY());
            if (Math.abs(translateX) < 0.00001) translateX = 0f;
            if (Math.abs(translateY) < 0.00001) translateY = 0f;
            fontisTransformed = false;
        }
    }

    boolean directToPS = !fontisTransformed;

    if (!PSPrinterJob.shapeTextProp && directToPS) {

        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
        if (psPrinterJob.setFont(getFont())) {

            /* Set the text color.
             * We should not be in this shape printing path
             * if the application is drawing with non-solid
             * colors. We should be in the raster path. Because
             * we are here in the shape path, the cast of the
             * paint to a Color should be fine.
             */
            try {
                psPrinterJob.setColor((Color)getPaint());
            } catch (ClassCastException e) {
                if (oldFont != null) {
                    setFont(oldFont);
                }
                throw new IllegalArgumentException(
                                            "Expected a Color instance");
            }

            psPrinterJob.setTransform(getTransform());
            psPrinterJob.setClip(getClip());

            drawnWithPS = psPrinterJob.textOut(this, str,
                                               x+translateX, y+translateY,
                                               font, frc, w);
        }
    }

    /* The text could not be converted directly to PS text
     * calls so decompose the text into a shape.
     */
    if (drawnWithPS == false) {
        if (oldFont != null) {
            setFont(oldFont);
            oldFont = null;
        }
        super.drawString(str, x, y, font, frc, w);
    }

    if (oldFont != null) {
        setFont(oldFont);
    }
}
 
Example 18
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    if (str.length() == 0) {
        return;
    }

    /* If the Font has layout attributes we need to delegate to TextLayout.
     * TextLayout renders text as GlyphVectors. We try to print those
     * using printer fonts - ie using Postscript text operators so
     * we may be reinvoked. In that case the "!printingGlyphVector" test
     * prevents us recursing and instead sends us into the body of the
     * method where we can safely ignore layout attributes as those
     * are already handled by TextLayout.
     */
    if (font.hasLayoutAttributes() && !printingGlyphVector) {
        TextLayout layout = new TextLayout(str, font, frc);
        layout.draw(this, x, y);
        return;
    }

    Font oldFont = getFont();
    if (!oldFont.equals(font)) {
        setFont(font);
    } else {
        oldFont = null;
    }

    boolean drawnWithPS = false;

    float translateX = 0f, translateY = 0f;
    boolean fontisTransformed = getFont().isTransformed();

    if (fontisTransformed) {
        AffineTransform fontTx = getFont().getTransform();
        int transformType = fontTx.getType();
        /* TYPE_TRANSLATION is a flag bit but we can do "==" here
         * because we want to detect when its just that bit set and
         *
         */
        if (transformType == AffineTransform.TYPE_TRANSLATION) {
            translateX = (float)(fontTx.getTranslateX());
            translateY = (float)(fontTx.getTranslateY());
            if (Math.abs(translateX) < 0.00001) translateX = 0f;
            if (Math.abs(translateY) < 0.00001) translateY = 0f;
            fontisTransformed = false;
        }
    }

    boolean directToPS = !fontisTransformed;

    if (!PSPrinterJob.shapeTextProp && directToPS) {

        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
        if (psPrinterJob.setFont(getFont())) {

            /* Set the text color.
             * We should not be in this shape printing path
             * if the application is drawing with non-solid
             * colors. We should be in the raster path. Because
             * we are here in the shape path, the cast of the
             * paint to a Color should be fine.
             */
            try {
                psPrinterJob.setColor((Color)getPaint());
            } catch (ClassCastException e) {
                if (oldFont != null) {
                    setFont(oldFont);
                }
                throw new IllegalArgumentException(
                                            "Expected a Color instance");
            }

            psPrinterJob.setTransform(getTransform());
            psPrinterJob.setClip(getClip());

            drawnWithPS = psPrinterJob.textOut(this, str,
                                               x+translateX, y+translateY,
                                               font, frc, w);
        }
    }

    /* The text could not be converted directly to PS text
     * calls so decompose the text into a shape.
     */
    if (drawnWithPS == false) {
        if (oldFont != null) {
            setFont(oldFont);
            oldFont = null;
        }
        super.drawString(str, x, y, font, frc, w);
    }

    if (oldFont != null) {
        setFont(oldFont);
    }
}
 
Example 19
Source Project: Bytecoder   File: MarlinRenderingEngine.java    License: Apache License 2.0 4 votes vote down vote up
private float userSpaceLineWidth(AffineTransform at, float lw) {

        float widthScale;

        if (at == null) {
            widthScale = 1.0f;
        } else if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM  |
                                    AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
            // Determinant may be negative (flip), use its absolute value:
            widthScale = (float)Math.sqrt(Math.abs(at.getDeterminant()));
        } else {
            // First calculate the "maximum scale" of this transform.
            double A = at.getScaleX();       // m00
            double C = at.getShearX();       // m01
            double B = at.getShearY();       // m10
            double D = at.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */

            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2.0d * (A*C + B*D); // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */

            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
            // sqrt omitted, compare to squared limits below.
            double widthsquared = ((EA + EC + hypot) / 2.0d);

            widthScale = (float)Math.sqrt(widthsquared);
        }

        return (lw / widthScale);
    }
 
Example 20
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}