aboutsummaryrefslogtreecommitdiff
path: root/test/java/awt/font/TextLayout/VisibleAdvance.java
blob: 224b2386125388d4a54e99816dac824cee9835ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright 2005-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */


import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.text.*;

/* @test
 * @summary verify TextLine advance
 * @bug 6582460
 */

/*
 Sample correct output:
Left-to-right (One style): Advance = 127.30078, Visible advance = 118.30078
Right-to-left (One style): Advance = 127.30078, Visible advance = 118.30078
Left-to-right (Multiple styles): Advance = 127.30078, Visible advance = 118.30078
Right-to-left (Multiple styles): Advance = 127.30078, Visible advance = 118.30078
*/

public class VisibleAdvance
{
    public static void main (String [] args)
    {
        System.out.println ("java.version = " + System.getProperty ("java.version"));

        float advances[] = null;
        advances = showAndCalculateAdvance ("Left-to-right (One style): ", getString (TextAttribute.RUN_DIRECTION_LTR, false), advances);
        advances = showAndCalculateAdvance ("Right-to-left (One style): ", getString (TextAttribute.RUN_DIRECTION_RTL, false), advances);

        advances = showAndCalculateAdvance ("Left-to-right (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_LTR, true), advances);
        advances = showAndCalculateAdvance ("Right-to-left (Multiple styles): ", getString (TextAttribute.RUN_DIRECTION_RTL, true), advances);
    }

    private static final String textA = "Text with trailing ";
    private static final String textB = "spaces";
    private static final String textC = "   ";
    private static final String text = textA + textB + textC;

    private static final int startOfTextB = textA.length ();
    private static final int endOfTextB = startOfTextB + textB.length ();

    private static final Font font = new Font ("Serif", Font.PLAIN, 12);

    private static AttributedString getString (Boolean direction,
                                               boolean multipleStyles)
    {
        AttributedString as = new AttributedString (text);
        as.addAttribute (TextAttribute.FONT, font);
        as.addAttribute (TextAttribute.RUN_DIRECTION, direction);

        if (multipleStyles)
            as.addAttribute (TextAttribute.FOREGROUND, Color.RED, startOfTextB, endOfTextB);

        return as;
    }

    private static FontRenderContext fontRenderContext =
        new FontRenderContext (new AffineTransform (), true, true);

    /*
     * @param advances on input,  null or float[2]. On output:  { advance, visibleAdvance }
     * @param return new float array
     */
    private static float[] showAndCalculateAdvance (String what,
                                     AttributedString as,
                                     float advances[])
    {
        TextLayout layout = new TextLayout (as.getIterator (), fontRenderContext);

        System.out.println (what + "Advance = " + layout.getAdvance () +
                            ", Visible advance = " + layout.getVisibleAdvance ());
        float advance = layout.getAdvance();
        float visAdvance = layout.getVisibleAdvance();
        if(advances == null) {
           advances = new float[2];
        } else if( Float.compare(advances[0],advance)!=0 || Float.compare(advances[1],visAdvance)!=0) {
                throw new RuntimeException("MISMATCH in advance.. " + what + "Advance = " + layout.getAdvance () +
                            ", Visible advance = " + layout.getVisibleAdvance () + ", previous values were: ["+advances[0]+","+advances[1]+"]");
        }
        advances[0] = advance;
        advances[1] = visAdvance;
        return advances;
    }
}