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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* Copyright (c) 2004, 2018, 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.
*/
/* @test
@bug 4328745 5090704 8166111 8176510 8202767
@summary exercise getLayoutFlags, getGlyphCharIndex, getGlyphCharIndices
*/
import java.awt.Font;
import static java.awt.Font.*;
import java.awt.GraphicsEnvironment;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import static java.awt.font.GlyphVector.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
public class TestLayoutFlags {
static public void main(String[] args) {
new TestLayoutFlags().runTest();
}
void runTest() {
Font font = findFont("abcde");
if (font == null) {
return; // this system is no use for this test.
}
String latin1 = "This is a latin1 string"; // none
String hebrew = "\u05d0\u05d1\u05d2\u05d3"; // rtl
String arabic = "\u0646\u0644\u0622\u0646"; // rtl + mc/g
String hindi = "\u0939\u093f\u0923\u094d\u0921\u0940"; // ltr + reorder
String tamil = "\u0b9c\u0bcb"; // ltr + mg/c + split
FontRenderContext frc = new FontRenderContext(null, true, true);
// get glyph char indices needs to initializes layoutFlags before
// use (5090704)
{
GlyphVector gv = font.createGlyphVector(frc, "abcde");
int ix = gv.getGlyphCharIndex(0);
if (ix != 0) {
throw new Error("glyph 0 incorrectly mapped to char " + ix);
}
int[] ixs = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), null);
for (int i = 0; i < ixs.length; ++i) {
if (ixs[i] != i) {
throw new Error("glyph " + i + " incorrectly mapped to char " + ixs[i]);
}
}
}
GlyphVector latinGV = makeGlyphVector("latin", latin1, false, frc);
GlyphVector hebrewGV = makeGlyphVector("hebrew", hebrew, true, frc);
GlyphVector arabicGV = makeGlyphVector("arabic", arabic, true, frc);
GlyphVector hindiGV = makeGlyphVector("devanagari", hindi, false, frc);
GlyphVector tamilGV = makeGlyphVector("tamil", tamil, false, frc);
GlyphVector latinPos = font.createGlyphVector(frc, latin1);
Point2D pt = latinPos.getGlyphPosition(0);
pt.setLocation(pt.getX(), pt.getY() + 1.0);
latinPos.setGlyphPosition(0, pt);
GlyphVector latinTrans = font.createGlyphVector(frc, latin1);
latinTrans.setGlyphTransform(0, AffineTransform.getRotateInstance(.15));
test("latin", latinGV, true, 0);
test("hebrew", hebrewGV, true, FLAG_RUN_RTL);
test("arabic", arabicGV, true, FLAG_COMPLEX_GLYPHS | FLAG_RUN_RTL);
test("hindi", hindiGV, true, FLAG_COMPLEX_GLYPHS);
test("tamil", tamilGV, true, FLAG_COMPLEX_GLYPHS);
test("pos", latinPos, true, 0);
test("trans", latinTrans, false, 0);
}
static boolean isLogicalFont(Font f) {
String family = f.getFamily().toLowerCase();
switch (family) {
case "dialog":
case "dialoginput":
case "serif":
case "sansserif":
case "monospaced": return true;
default: return false;
}
}
Font[] allFonts = null;
Font findFont(String text) {
if (allFonts == null) {
allFonts =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
}
for (Font f : allFonts) {
if (isLogicalFont(f)) {
continue;
}
if (f.canDisplayUpTo(text) == -1) {
return f.deriveFont(Font.PLAIN, 24);
}
}
return null;
}
GlyphVector makeGlyphVector(String script, String text,
boolean rtl, FontRenderContext frc) {
Font font = findFont(text);
if (font == null) {
System.out.println("No font found for script " + script);
return null;
} else {
System.out.println("Using " + font.getFontName() +
" for script " + script);
}
int flags = rtl ? LAYOUT_RIGHT_TO_LEFT : LAYOUT_LEFT_TO_RIGHT;
return font.layoutGlyphVector(frc, text.toCharArray(),
0, text.length(), flags);
}
void test(String name, GlyphVector gv, boolean layout, int allowedFlags) {
if (gv == null) {
return;
}
int iv = (layout) ? FLAG_HAS_POSITION_ADJUSTMENTS : 0;
int computedFlags = computeFlags(gv, iv) & gv.FLAG_MASK;
int actualFlags = gv.getLayoutFlags() & gv.FLAG_MASK;
System.out.println("\n*** " + name + " ***");
System.out.println(" test flags");
System.out.print("computed ");
printFlags(computedFlags);
System.out.print(" actual ");
printFlags(actualFlags);
System.out.print("allowed layout ");
printFlags(allowedFlags);
if (computedFlags != actualFlags) {
// Depending on the font, if layout is run for a RTL script
// you might get that flag set, or COMPLEX_GLYPHS instead.
boolean error = false;
int COMPLEX_RTL = FLAG_COMPLEX_GLYPHS | FLAG_RUN_RTL;
if (allowedFlags == 0) {
error = (allowedFlags & COMPLEX_RTL) != 0;
}
if (allowedFlags == FLAG_RUN_RTL) {
error = (actualFlags & FLAG_COMPLEX_GLYPHS) != 0;
}
if (allowedFlags == FLAG_COMPLEX_GLYPHS) {
error = (actualFlags & FLAG_RUN_RTL) != 0;
}
if (allowedFlags == COMPLEX_RTL) {
error = (actualFlags & COMPLEX_RTL) == 0;
}
if (error) {
throw new Error("layout flags in test: " + name +
" expected: " + Integer.toHexString(computedFlags) +
" but got: " + Integer.toHexString(actualFlags));
}
}
}
static public void printFlags(int flags) {
System.out.print("flags:");
if ((flags & FLAG_HAS_POSITION_ADJUSTMENTS) != 0) {
System.out.print(" pos");
}
if ((flags & FLAG_HAS_TRANSFORMS) != 0) {
System.out.print(" trans");
}
if ((flags & FLAG_RUN_RTL) != 0) {
System.out.print(" rtl");
}
if ((flags & FLAG_COMPLEX_GLYPHS) != 0) {
System.out.print(" complex");
}
if ((flags & FLAG_MASK) == 0) {
System.out.print(" none");
}
System.out.println();
}
int computeFlags(GlyphVector gv, int initValue) {
validateCharIndexMethods(gv);
int result = initValue;
if (glyphsAreRTL(gv)) {
result |= FLAG_RUN_RTL;
}
if (hasComplexGlyphs(gv)) {
result |= FLAG_COMPLEX_GLYPHS;
}
if (gv.getFont().isTransformed()) {
result |= FLAG_HAS_TRANSFORMS;
}
return result;
}
/**
* throw an exception if getGlyphCharIndices returns a different result than
* you get from iterating through getGlyphCharIndex one at a time.
*/
void validateCharIndexMethods(GlyphVector gv) {
int[] indices = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), null);
for (int i = 0; i < gv.getNumGlyphs(); ++i) {
if (gv.getGlyphCharIndex(i) != indices[i]) {
throw new Error("glyph index mismatch at " + i);
}
}
}
/**
* Return true if the glyph indices are pure ltr
*/
boolean glyphsAreLTR(GlyphVector gv) {
int[] indices = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), null);
for (int i = 0; i < indices.length; ++i) {
if (indices[i] != i) {
return false;
}
}
return true;
}
/**
* Return true if the glyph indices are pure rtl
*/
boolean glyphsAreRTL(GlyphVector gv) {
int[] indices = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), null);
for (int i = 0; i < indices.length; ++i) {
if (indices[i] != indices.length - i - 1) {
return false;
}
}
return true;
}
/**
* Return true if there is a local reordering (the run is not ltr or rtl).
* !!! We can't have mixed bidi runs in the glyphs.
*/
boolean hasComplexGlyphs(GlyphVector gv) {
return !glyphsAreLTR(gv) && !glyphsAreRTL(gv);
}
}
|