427N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
427N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
427N/A *
427N/A * This code is free software; you can redistribute it and/or modify it
427N/A * under the terms of the GNU General Public License version 2 only, as
427N/A * published by the Free Software Foundation.
427N/A *
427N/A * This code is distributed in the hope that it will be useful, but WITHOUT
427N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
427N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
427N/A * version 2 for more details (a copy is included in the LICENSE file that
427N/A * accompanied this code).
427N/A *
427N/A * You should have received a copy of the GNU General Public License version
427N/A * 2 along with this work; if not, write to the Free Software Foundation,
427N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
427N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
427N/A */
427N/A
427N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.font.*;
0N/Aimport java.awt.geom.*;
0N/Aimport java.text.*;
0N/A
0N/A/* @test
0N/A * @summary verify TextLine advance
0N/A * @bug 6582460
0N/A */
0N/A
0N/A/*
0N/A Sample correct output:
0N/ALeft-to-right (One style): Advance = 127.30078, Visible advance = 118.30078
0N/ARight-to-left (One style): Advance = 127.30078, Visible advance = 118.30078
0N/ALeft-to-right (Multiple styles): Advance = 127.30078, Visible advance = 118.30078
0N/ARight-to-left (Multiple styles): Advance = 127.30078, Visible advance = 118.30078
0N/A*/
0N/A
0N/Apublic class VisibleAdvance
0N/A{
0N/A public static void main (String [] args)
0N/A {
0N/A System.out.println ("java.version = " + System.getProperty ("java.version"));
0N/A
0N/A float advances[] = null;
0N/A advances = showAndCalculateAdvance ("Left-to-right (One style): ", getString (TextAttribute.RUN_DIRECTION_LTR, false), advances);
0N/A advances = showAndCalculateAdvance ("Right-to-left (One style): ", getString (TextAttribute.RUN_DIRECTION_RTL, false), advances);
0N/A
0N/A advances = showAndCalculateAdvance ("Left-to-right (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_LTR, true), advances);
0N/A advances = showAndCalculateAdvance ("Right-to-left (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_RTL, true), advances);
0N/A }
0N/A
0N/A private static final String textA = "Text with trailing ";
0N/A private static final String textB = "spaces";
0N/A private static final String textC = " ";
0N/A private static final String text = textA + textB + textC;
0N/A
0N/A private static final int startOfTextB = textA.length ();
0N/A private static final int endOfTextB = startOfTextB + textB.length ();
0N/A
0N/A private static final Font font = new Font ("Serif", Font.PLAIN, 12);
0N/A
0N/A private static AttributedString getString (Boolean direction,
0N/A boolean multipleStyles)
0N/A {
0N/A AttributedString as = new AttributedString (text);
0N/A as.addAttribute (TextAttribute.FONT, font);
0N/A as.addAttribute (TextAttribute.RUN_DIRECTION, direction);
0N/A
0N/A if (multipleStyles)
0N/A as.addAttribute (TextAttribute.FOREGROUND, Color.RED, startOfTextB, endOfTextB);
0N/A
0N/A return as;
0N/A }
0N/A
0N/A private static FontRenderContext fontRenderContext =
0N/A new FontRenderContext (new AffineTransform (), true, true);
0N/A
0N/A /*
0N/A * @param advances on input, null or float[2]. On output: { advance, visibleAdvance }
0N/A * @param return new float array
0N/A */
0N/A private static float[] showAndCalculateAdvance (String what,
0N/A AttributedString as,
0N/A float advances[])
0N/A {
0N/A TextLayout layout = new TextLayout (as.getIterator (), fontRenderContext);
0N/A
0N/A System.out.println (what + "Advance = " + layout.getAdvance () +
0N/A ", Visible advance = " + layout.getVisibleAdvance ());
0N/A float advance = layout.getAdvance();
0N/A float visAdvance = layout.getVisibleAdvance();
0N/A if(advances == null) {
0N/A advances = new float[2];
0N/A } else if( Float.compare(advances[0],advance)!=0 || Float.compare(advances[1],visAdvance)!=0) {
0N/A throw new RuntimeException("MISMATCH in advance.. " + what + "Advance = " + layout.getAdvance () +
0N/A ", Visible advance = " + layout.getVisibleAdvance () + ", previous values were: ["+advances[0]+","+advances[1]+"]");
0N/A }
0N/A advances[0] = advance;
0N/A advances[1] = visAdvance;
0N/A return advances;
0N/A }
0N/A}