Java Code Examples for java.awt.geom.AffineTransform#TYPE_TRANSLATION
The following examples show how to use
java.awt.geom.AffineTransform#TYPE_TRANSLATION .
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: SunGraphics2D.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 2
Source File: SunGraphics2D.java From Bytecoder with Apache License 2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 3
Source File: NativeStrike.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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
Source File: HiDPIUtils.java From netbeans with Apache License 2.0 | 5 votes |
/** * Paint at scale factor 1x to avoid rounding issues at 125%, 150% and 175% scaling. * <p> * Scales the given Graphics2D down to 100% and invokes the * given painter passing scaled deviceWidth and deviceHeight. * <p> * Transformation to device pixels borrowed from class * {@link org.netbeans.swing.plaf.windows8.DPISafeBorder}. */ public static void paintAtScale1x(Graphics g0, int x, int y, int width, int height, HiDPIPainter painter) { Graphics2D g = (Graphics2D) g0; final AffineTransform oldTransform = g.getTransform(); g.translate(x, y); final AffineTransform tx = g.getTransform(); final int txType = tx.getType(); /* On fractional DPI scaling factors, such as 150%, a logical pixel position, e.g. (5,0), may end up being translated to a non-integral device pixel position, e.g. (7.5, 0). The same goes for border thicknesses, which are specified in logical pixels. In this method, we do all calculations and painting in device pixels, to avoid rounding errors causing visible artifacts. On screens without HiDPI scaling, logical pixel values and device pixel values are identical, and always integral (whole number) values. */ if (txType == AffineTransform.TYPE_UNIFORM_SCALE || txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION)) { // HiDPI scaling is active. double scale = tx.getScaleX(); /* Round the starting (top-left) position up and the end (bottom-right) position down, to ensure we are painting the border in an area that will not be painted over by an adjacent component. */ int deviceX = (int) Math.ceil(tx.getTranslateX()); int deviceY = (int) Math.ceil(tx.getTranslateY()); int deviceXend = (int) (tx.getTranslateX() + width * scale); int deviceYend = (int) (tx.getTranslateY() + height * scale); int deviceWidth = deviceXend - deviceX; int deviceHeight = deviceYend - deviceY; /* Deactivate the HiDPI scaling transform so we can do paint operations in the device pixel coordinate system instead of in the logical coordinate system. */ g.setTransform(new AffineTransform(1, 0, 0, 1, deviceX, deviceY)); painter.paint(g, deviceWidth, deviceHeight, scale); } else { painter.paint(g, width, height, 1); } g.setTransform(oldTransform); }
Example 5
Source File: SunGraphics2D.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 6
Source File: SunGraphics2D.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 7
Source File: SunGraphics2D.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 8
Source File: SunGraphics2D.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the given AffineTransform is an integer * translation. */ private static boolean isIntegerTranslation(AffineTransform xform) { if (xform.isIdentity()) { return true; } if (xform.getType() == AffineTransform.TYPE_TRANSLATION) { double tx = xform.getTranslateX(); double ty = xform.getTranslateY(); return (tx == (int)tx && ty == (int)ty); } return false; }
Example 9
Source File: NativeStrike.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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 10
Source File: NativeStrike.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
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 11
Source File: NativeStrike.java From hottub with GNU General Public License v2.0 | 5 votes |
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 12
Source File: NativeStrike.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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 13
Source File: DPIUnscaledBorder.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void paintBorder(Component c, Graphics g0, int x, int y, int width, int height) { final Graphics2D g = (Graphics2D) g0; final AffineTransform oldTransform = g.getTransform(); g.translate(x, y); final AffineTransform tx = g.getTransform(); final int txType = tx.getType(); /* On fractional DPI scaling factors, such as 150%, a logical pixel position, e.g. (5,0), may end up being translated to a non-integral device pixel position, e.g. (7.5, 0). The same goes for border thicknesses, which are specified in logical pixels. In this method, we do all calculations and painting in device pixels, to avoid rounding errors causing visible artifacts. On screens without HiDPI scaling, logical pixel values and device pixel values are identical, and always integral (whole number) values. */ final int deviceWidth; final int deviceHeight; if (txType == AffineTransform.TYPE_UNIFORM_SCALE || txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION)) { // HiDPI scaling is active. double scale = tx.getScaleX(); /* To be completely safe from overpainting the previous adjacent component, we would probably need to round up here. But for borders to work properly on JTextField, we must round down. And it seems to work fine in the EDITOR_TAB_CONTENT_BORDER/VIEW_TAB_CONTENT_BORDER cases as well. */ int deviceX = (int) tx.getTranslateX(); int deviceY = (int) tx.getTranslateY(); /* Rounding down here should guarantee that we do not paint in an area that will be painted over by the next adjacent component. Rounding up, or to the nearest integer, is confirmed to cause problems. */ int deviceXend = (int) (tx.getTranslateX() + width * scale); int deviceYend = (int) (tx.getTranslateY() + height * scale); deviceWidth = deviceXend - deviceX; deviceHeight = deviceYend - deviceY; /* Deactivate the HiDPI scaling transform so we can do paint operations in the device pixel coordinate system instead of in the logical coordinate system. */ g.setTransform(new AffineTransform(1, 0, 0, 1, deviceX, deviceY)); } else { deviceWidth = width; deviceHeight = height; } delegate.paintBorder(c, g, 0, 0, deviceWidth, deviceHeight); g.setTransform(oldTransform); }
Example 14
Source File: VectorIcon.java From netbeans with Apache License 2.0 | 4 votes |
@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
Source File: PSPathGraphics.java From hottub with GNU General Public License v2.0 | 4 votes |
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 16
Source File: PSPathGraphics.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
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 17
Source File: SunGraphics2D.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
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 18
Source File: PSPathGraphics.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
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 File: PSPathGraphics.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
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 20
Source File: SunGraphics2D.java From Bytecoder with Apache License 2.0 | 4 votes |
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); } }