1/* 2 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25import java.awt.*; 26import java.awt.font.*; 27import java.awt.geom.*; 28import java.text.*; 29 30/* @test 31 * @summary verify TextLine advance 32 * @bug 6582460 8164818 33 */ 34 35/* 36 Sample correct output: 37Left-to-right (One style): Advance = 127.30078, Visible advance = 118.30078 38Right-to-left (One style): Advance = 127.30078, Visible advance = 118.30078 39Left-to-right (Multiple styles): Advance = 127.30078, Visible advance = 118.30078 40Right-to-left (Multiple styles): Advance = 127.30078, Visible advance = 118.30078 41*/ 42 43public class VisibleAdvance 44{ 45 public static void main (String [] args) 46 { 47 System.out.println ("java.version = " + System.getProperty ("java.version")); 48 49 float advances[] = null; 50 advances = showAndCalculateAdvance ("Left-to-right (One style): ", getString (TextAttribute.RUN_DIRECTION_LTR, false), advances); 51 advances = showAndCalculateAdvance ("Right-to-left (One style): ", getString (TextAttribute.RUN_DIRECTION_RTL, false), advances); 52 53 advances = showAndCalculateAdvance ("Left-to-right (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_LTR, true), advances); 54 advances = showAndCalculateAdvance ("Right-to-left (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_RTL, true), advances); 55 } 56 57 private static final String textA = "Text with trailing "; 58 private static final String textB = "spaces"; 59 private static final String textC = " "; 60 private static final String text = textA + textB + textC; 61 62 private static final int startOfTextB = textA.length (); 63 private static final int endOfTextB = startOfTextB + textB.length (); 64 65 private static final Font font = new Font ("Serif", Font.PLAIN, 12); 66 67 private static AttributedString getString (Boolean direction, 68 boolean multipleStyles) 69 { 70 AttributedString as = new AttributedString (text); 71 as.addAttribute (TextAttribute.FONT, font); 72 as.addAttribute (TextAttribute.RUN_DIRECTION, direction); 73 74 if (multipleStyles) 75 as.addAttribute (TextAttribute.FOREGROUND, Color.RED, startOfTextB, endOfTextB); 76 77 return as; 78 } 79 80 private static FontRenderContext fontRenderContext = 81 new FontRenderContext (new AffineTransform (), true, true); 82 83 /* 84 * @param advances on input, null or float[2]. On output: { advance, visibleAdvance } 85 * @param return new float array 86 */ 87 private static float[] showAndCalculateAdvance (String what, 88 AttributedString as, 89 float advances[]) 90 { 91 TextLayout layout = new TextLayout (as.getIterator (), fontRenderContext); 92 93 System.out.println (what + "Advance = " + layout.getAdvance () + 94 ", Visible advance = " + layout.getVisibleAdvance ()); 95 float advance = layout.getAdvance(); 96 float visAdvance = layout.getVisibleAdvance(); 97 if(advances == null) { 98 advances = new float[2]; 99 } else if( Float.compare(advances[0],advance)!=0 || Float.compare(advances[1],visAdvance)!=0) { 100 throw new RuntimeException("MISMATCH in advance.. " + what + "Advance = " + layout.getAdvance () + 101 ", Visible advance = " + layout.getVisibleAdvance () + ", previous values were: ["+advances[0]+","+advances[1]+"]"); 102 } 103 advances[0] = advance; 104 advances[1] = visAdvance; 105 return advances; 106 } 107} 108