Changeset 8994 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2015-11-07T13:24:20+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r8933 r8994 6 6 7 7 import java.awt.Color; 8 import java.awt.Font; 8 9 import java.awt.Toolkit; 9 10 import java.awt.datatransfer.Clipboard; … … 13 14 import java.awt.datatransfer.Transferable; 14 15 import java.awt.datatransfer.UnsupportedFlavorException; 16 import java.awt.font.FontRenderContext; 17 import java.awt.font.GlyphVector; 15 18 import java.io.BufferedReader; 16 19 import java.io.ByteArrayOutputStream; … … 34 37 import java.security.MessageDigest; 35 38 import java.security.NoSuchAlgorithmException; 39 import java.text.Bidi; 36 40 import java.text.MessageFormat; 37 41 import java.util.AbstractCollection; … … 1493 1497 return hashMapInitialCapacity(nEntries, 0.75f); 1494 1498 } 1499 1500 /** 1501 * Utility class to save a string along with its rendering direction 1502 * (left-to-right or right-to-left). 1503 */ 1504 private static class DirectionString { 1505 public int direction; 1506 public String str; 1507 1508 public DirectionString(int direction, String str) { 1509 this.direction = direction; 1510 this.str = str; 1511 } 1512 } 1513 1514 /** 1515 * Convert a string to a list of {@link GlyphVector}s. The string may contain 1516 * bi-directional text. The result will be in correct visual order. 1517 * Each element of the resulting list corresponds to one section of the 1518 * string with consistent writing direction (left-to-right or right-to-left). 1519 * 1520 * @param string the string to render 1521 * @param font the font 1522 * @param frc a FontRenderContext object 1523 * @return a list of GlyphVectors 1524 */ 1525 public static List<GlyphVector> getGlyphVectorsBidi(String string, Font font, FontRenderContext frc) { 1526 List<GlyphVector> gvs = new ArrayList<>(); 1527 Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); 1528 byte[] levels = new byte[bidi.getRunCount()]; 1529 DirectionString[] dirStrings = new DirectionString[levels.length]; 1530 for (int i = 0; i < levels.length; ++i) { 1531 levels[i] = (byte) bidi.getRunLevel(i); 1532 String substr = string.substring(bidi.getRunStart(i), bidi.getRunLimit(i)); 1533 int dir = levels[i] % 2 == 0 ? Bidi.DIRECTION_LEFT_TO_RIGHT : Bidi.DIRECTION_RIGHT_TO_LEFT; 1534 dirStrings[i] = new DirectionString(dir, substr); 1535 } 1536 Bidi.reorderVisually(levels, 0, dirStrings, 0, levels.length); 1537 for (int i = 0; i < dirStrings.length; ++i) { 1538 char[] chars = dirStrings[i].str.toCharArray(); 1539 gvs.add(font.layoutGlyphVector(frc, chars, 0, chars.length, dirStrings[i].direction)); 1540 } 1541 return gvs; 1542 } 1543 1495 1544 }
Note:
See TracChangeset
for help on using the changeset viewer.