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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (c) 1999, 2025, 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.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
import javax.swing.JPanel;
/*
* @test
* @bug 4221422
* @summary Display several TextLayouts with various selections.
* All the selections should be between non-italic and italic text,
* and the top and bottom of the selection region should be horizontal.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual TestSelection
*/
public final class TestSelection extends JPanel {
private static final float MARGIN = 20;
public static void main(String[] args) throws Exception {
final String INSTRUCTIONS = """
Several TextLayouts are displayed along with selections.
The selection regions should have horizontal top and bottom segments.
If above condition is true, press Pass else Fail.""";
PassFailJFrame.builder()
.title("TestSelection Instruction")
.instructions(INSTRUCTIONS)
.columns(40)
.splitUI(TestSelection::new)
.build()
.awaitAndCheck();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
private float drawSelectionAndLayout(Graphics2D g2d,
TextLayout layout,
float y,
int selStart,
int selLimit) {
Color selectionColor = Color.PINK;
Color textColor = Color.BLACK;
y += layout.getAscent();
g2d.translate(MARGIN, y);
Shape hl = layout.getLogicalHighlightShape(selStart, selLimit);
g2d.setColor(selectionColor);
g2d.fill(hl);
g2d.setColor(textColor);
layout.draw(g2d, 0, 0);
g2d.translate(-MARGIN, -y);
y += layout.getDescent() + layout.getLeading() + 10;
return y;
}
@Override
public void paint(Graphics g) {
String text = "Hello world";
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
AttributedString attrStr = new AttributedString(text);
FontRenderContext frc = g2d.getFontRenderContext();
final int midPoint = text.indexOf('w');
final int selStart = midPoint / 2;
final int selLimit = text.length() - selStart;
final Font italic = new Font(Font.SANS_SERIF, Font.ITALIC, 24);
float y = MARGIN;
attrStr.addAttribute(TextAttribute.FONT, italic, 0, midPoint);
TextLayout layout = new TextLayout(attrStr.getIterator(), frc);
y = drawSelectionAndLayout(g2d, layout, y, selStart - 1, selLimit);
y = drawSelectionAndLayout(g2d, layout, y, selStart, selLimit);
y = drawSelectionAndLayout(g2d, layout, y, selStart + 1, selLimit);
attrStr = new AttributedString(text);
attrStr.addAttribute(TextAttribute.FONT,
italic, midPoint, text.length());
layout = new TextLayout(attrStr.getIterator(), frc);
y = drawSelectionAndLayout(g2d, layout, y, selStart, selLimit);
attrStr = new AttributedString(text);
attrStr.addAttribute(TextAttribute.FONT, italic, 0, midPoint);
attrStr.addAttribute(TextAttribute.SIZE, 48f, midPoint, text.length());
layout = new TextLayout(attrStr.getIterator(), frc);
y = drawSelectionAndLayout(g2d, layout, y, selStart, selLimit);
}
}
|