--- old/src/java.desktop/share/classes/sun/java2d/pipe/GlyphListPipe.java 2016-06-23 22:23:57.092777900 +0300 +++ new/src/java.desktop/share/classes/sun/java2d/pipe/GlyphListPipe.java 2016-06-23 22:23:56.700397600 +0300 @@ -53,8 +53,8 @@ if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) { double origin[] = {x + info.originX, y + info.originY}; sg2d.transform.transform(origin, 0, origin, 0, 1); - devx = (float)origin[0]; - devy = (float)origin[1]; + devx = (float) closeToHalfInteger(origin[0]); + devy = (float) closeToHalfInteger(origin[1]); } else { devx = (float)(x + info.originX + sg2d.transX); devy = (float)(y + info.originY + sg2d.transY); @@ -75,6 +75,17 @@ } } + private static final double MAX_TX_ERROR = .0001; + + private static double closeToHalfInteger(double value) { + double d = value + 0.5; + int i = (int) Math.floor(d + 0.5); + if (Math.abs(d - i) < MAX_TX_ERROR) { + return i - 0.5; + } + return value; + } + public void drawChars(SunGraphics2D sg2d, char data[], int offset, int length, int ix, int iy) --- /dev/null 2016-06-23 22:23:59.000000000 +0300 +++ new/test/java/awt/Graphics2D/DrawString/DrawScaledText.java 2016-06-23 22:23:58.890503800 +0300 @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING; +import static java.awt.RenderingHints.KEY_TEXT_LCD_CONTRAST; +import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB; +import java.awt.font.FontRenderContext; + +/** + * @test + * @bug 8158370 + * @summary Text drawn from float pointing position and with float pointing + * scale is shifted + */ +public class DrawScaledText { + + private static final Color TEXT_COLOR = Color.BLACK; + private static final Color SELECTED_TEXT_COLOR = Color.RED; + private static final int IMG_WIDTH = 230; + private static final int IMG_HEIGHT = 60; + + public static void main(String[] args) throws Exception { + + testScaledText(1.5, 1, 2, "aaaaaaaaaaaaaaaa", 154, 26); + testScaledText(2.25, 0, 2, "aa", 19, 40); + } + + private static void testScaledText(double scale, int translate, int x, + String text, int testX, int testY) + { + BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, + BufferedImage.TYPE_INT_RGB); + Graphics2D g = img.createGraphics(); + g.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_LCD_HRGB); + g.setRenderingHint(KEY_TEXT_LCD_CONTRAST, 120); + g.setColor(Color.WHITE); + g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT); + + // Important note: scale and translate the graphics + g.scale(scale, scale); + g.translate(translate, 0); + int y = 10; + + g.setColor(TEXT_COLOR); + g.drawString(text, x, y); + + y = 2 * y; + + Font font = g.getFont(); + int index = text.length() - 1; + FontRenderContext frc = g.getFontMetrics().getFontRenderContext(); + Rectangle2D rect = font.getStringBounds(text, 0, index, frc); + float selectedTextPosition = (float) rect.getWidth(); + g.drawString(text.substring(0, index), x, y); + g.setColor(SELECTED_TEXT_COLOR); + g.drawString(text.substring(index, text.length()), x + selectedTextPosition, y); + + // draw a circle around the test point + g.setTransform(new AffineTransform()); + g.setColor(Color.BLUE); + int r = 3; + g.drawOval(testX - r, testY - r, 2 * r, 2 * r); + g.dispose(); + + int rgb = img.getRGB(testX, testY); + if (Color.WHITE.getRGB() != rgb) { + throw new RuntimeException("Selected text is shifted to the left!"); + } + } +}