Java Code Examples for java.awt.geom.AffineTransform#TYPE_GENERAL_TRANSFORM

The following examples show how to use java.awt.geom.AffineTransform#TYPE_GENERAL_TRANSFORM . 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
Source File: ProxyGraphics2D.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 2
Source File: WPathGraphics.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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
Source File: ProxyGraphics2D.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return true if drawing {@code img} 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 4
Source File: WPathGraphics.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected int platformFontCount(Font font, String str) {

    AffineTransform deviceTransform = getTransform();
    AffineTransform fontTransform = new AffineTransform(deviceTransform);
    fontTransform.concatenate(getFont().getTransform());
    int transformType = fontTransform.getType();

    /* Test if GDI can handle the transform */
    boolean directToGDI = ((transformType !=
                           AffineTransform.TYPE_GENERAL_TRANSFORM)
                           && ((transformType & AffineTransform.TYPE_FLIP)
                               == 0));

    if (!directToGDI) {
        return 0;
    }

    /* Since all windows fonts are available, and the JRE fonts
     * are also registered. Only the Font.createFont() case is presently
     * unknown to GDI. Those can be registered too, although that
     * code does not exist yet, it can be added too, so we should not
     * fail that case. Just do a quick check whether its a TrueTypeFont
     * - ie not a Type1 font etc, and let drawString() resolve the rest.
     */
    Font2D font2D = FontUtilities.getFont2D(font);
    if (font2D instanceof CompositeFont ||
        font2D instanceof TrueTypeFont) {
        return 1;
    } else {
        return 0;
    }
}
 
Example 5
Source File: ProxyGraphics2D.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 6
Source File: WPathGraphics.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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 7
Source File: SunGraphics2D.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected void invalidateTransform() {
    int type = transform.getType();
    int origTransformState = transformState;
    if (type == AffineTransform.TYPE_IDENTITY) {
        transformState = TRANSFORM_ISIDENT;
        transX = transY = 0;
    } else if (type == AffineTransform.TYPE_TRANSLATION) {
        double dtx = transform.getTranslateX();
        double dty = transform.getTranslateY();
        transX = (int) Math.floor(dtx + 0.5);
        transY = (int) Math.floor(dty + 0.5);
        if (dtx == transX && dty == transY) {
            transformState = TRANSFORM_INT_TRANSLATE;
        } else {
            transformState = TRANSFORM_ANY_TRANSLATE;
        }
    } else if ((type & (AffineTransform.TYPE_FLIP |
                        AffineTransform.TYPE_MASK_ROTATION |
                        AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0)
    {
        transformState = TRANSFORM_TRANSLATESCALE;
        transX = transY = 0;
    } else {
        transformState = TRANSFORM_GENERIC;
        transX = transY = 0;
    }

    if (transformState >= TRANSFORM_TRANSLATESCALE ||
        origTransformState >= TRANSFORM_TRANSLATESCALE)
    {
        /* Its only in this case that the previous or current transform
         * was more than a translate that font info is invalidated
         */
        cachedFRC = null;
        this.validFontInfo = false;
        this.fontMetrics = null;
        this.glyphVectorFontInfo = null;

        if (transformState != origTransformState) {
            invalidatePipe();
        }
    }
    if (strokeState != STROKE_CUSTOM) {
        validateBasicStroke((BasicStroke) stroke);
    }
}
 
Example 8
Source File: XRUtils.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}
 
Example 9
Source File: PiscesRenderingEngine.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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 10
Source File: PiscesRenderingEngine.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: SunGraphics2D.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected void invalidateTransform() {
    int type = transform.getType();
    int origTransformState = transformState;
    if (type == AffineTransform.TYPE_IDENTITY) {
        transformState = TRANSFORM_ISIDENT;
        transX = transY = 0;
    } else if (type == AffineTransform.TYPE_TRANSLATION) {
        double dtx = transform.getTranslateX();
        double dty = transform.getTranslateY();
        transX = (int) Math.floor(dtx + 0.5);
        transY = (int) Math.floor(dty + 0.5);
        if (dtx == transX && dty == transY) {
            transformState = TRANSFORM_INT_TRANSLATE;
        } else {
            transformState = TRANSFORM_ANY_TRANSLATE;
        }
    } else if ((type & (AffineTransform.TYPE_FLIP |
                        AffineTransform.TYPE_MASK_ROTATION |
                        AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0)
    {
        transformState = TRANSFORM_TRANSLATESCALE;
        transX = transY = 0;
    } else {
        transformState = TRANSFORM_GENERIC;
        transX = transY = 0;
    }

    if (transformState >= TRANSFORM_TRANSLATESCALE ||
        origTransformState >= TRANSFORM_TRANSLATESCALE)
    {
        /* Its only in this case that the previous or current transform
         * was more than a translate that font info is invalidated
         */
        cachedFRC = null;
        this.validFontInfo = false;
        this.fontMetrics = null;
        this.glyphVectorFontInfo = null;

        if (transformState != origTransformState) {
            invalidatePipe();
        }
    }
    if (strokeState != STROKE_CUSTOM) {
        validateBasicStroke((BasicStroke) stroke);
    }
}
 
Example 12
Source File: WPathGraphics.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
* 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 13
Source File: XRUtils.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}
 
Example 14
Source File: Font2D.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private FontStrike getStrike(FontStrikeDesc desc, boolean copy) {
    /* Before looking in the map, see if the descriptor matches the
     * last strike returned from this Font2D. This should often be a win
     * since its common for the same font, in the same size to be
     * used frequently, for example in many parts of a UI.
     *
     * If its not the same then we use the descriptor to locate a
     * Reference to the strike. If it exists and points to a strike,
     * then we update the last strike to refer to that and return it.
     *
     * If the key isn't in the map, or its reference object has been
     * collected, then we create a new strike, put it in the map and
     * set it to be the last strike.
     */
    FontStrike strike = lastFontStrike.get();
    if (strike != null && desc.equals(strike.desc)) {
        return strike;
    } else {
        Reference<FontStrike> strikeRef = strikeCache.get(desc);
        if (strikeRef != null) {
            strike = strikeRef.get();
            if (strike != null) {
                updateLastStrikeRef(strike);
                StrikeCache.refStrike(strike);
                return strike;
            }
        }
        /* When we create a new FontStrike instance, we *must*
         * ask the StrikeCache for a reference. We must then ensure
         * this reference remains reachable, by storing it in the
         * Font2D's strikeCache map.
         * So long as the Reference is there (reachable) then if the
         * reference is cleared, it will be enqueued for disposal.
         * If for some reason we explicitly remove this reference, it
         * must only be done when holding a strong reference to the
         * referent (the FontStrike), or if the reference is cleared,
         * then we must explicitly "dispose" of the native resources.
         * The only place this currently happens is in this same method,
         * where we find a cleared reference and need to overwrite it
         * here with a new reference.
         * Clearing the whilst holding a strong reference, should only
         * be done if the
         */
        if (copy) {
            desc = new FontStrikeDesc(desc);
        }
        strike = createStrike(desc);
        //StrikeCache.addStrike();
        /* If we are creating many strikes on this font which
         * involve non-quadrant rotations, or more general
         * transforms which include shears, then force the use
         * of weak references rather than soft references.
         * This means that it won't live much beyond the next GC,
         * which is what we want for what is likely a transient strike.
         */
        int txType = desc.glyphTx.getType();
        if (useWeak ||
            txType == AffineTransform.TYPE_GENERAL_TRANSFORM ||
            (txType & AffineTransform.TYPE_GENERAL_ROTATION) != 0 &&
            strikeCache.size() > 10) {
            strikeRef = StrikeCache.getStrikeRef(strike, true);
        } else {
            strikeRef = StrikeCache.getStrikeRef(strike, useWeak);
        }
        strikeCache.put(desc, strikeRef);
        updateLastStrikeRef(strike);
        StrikeCache.refStrike(strike);
        return strike;
    }
}
 
Example 15
Source File: PiscesRenderingEngine.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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 16
Source File: XRUtils.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static boolean isTransformQuadrantRotated(AffineTransform tr) {
    return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
}
 
Example 17
Source File: PiscesRenderingEngine.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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 18
Source File: WPathGraphics.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
* 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 19
Source File: PiscesRenderingEngine.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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 20
Source File: WPathGraphics.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
* Draw the bounding rectangle using transformed coordinates.
*/
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));
       }
   }
}