Ignore:
Timestamp:
2015-11-07T13:24:20+01:00 (9 years ago)
Author:
bastiK
Message:

fixed #12025 - text along line rendering for bi-directional text

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8933 r8994  
    66
    77import java.awt.Color;
     8import java.awt.Font;
    89import java.awt.Toolkit;
    910import java.awt.datatransfer.Clipboard;
     
    1314import java.awt.datatransfer.Transferable;
    1415import java.awt.datatransfer.UnsupportedFlavorException;
     16import java.awt.font.FontRenderContext;
     17import java.awt.font.GlyphVector;
    1518import java.io.BufferedReader;
    1619import java.io.ByteArrayOutputStream;
     
    3437import java.security.MessageDigest;
    3538import java.security.NoSuchAlgorithmException;
     39import java.text.Bidi;
    3640import java.text.MessageFormat;
    3741import java.util.AbstractCollection;
     
    14931497        return hashMapInitialCapacity(nEntries, 0.75f);
    14941498    }
     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
    14951544}
Note: See TracChangeset for help on using the changeset viewer.