List of usage examples for java.text Bidi getRunCount
public int getRunCount()
From source file:com.repeatability.pdf.PDFTextStripper.java
/** * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given * word. If the word is a full line, the results will be the best. If the word contains of single words or * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and * characters!/*from w ww. jav a2s .co m*/ * * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx * * @param word The word that shall be processed * @return new word with the correct direction of the containing characters */ // kwa //private String handleDirection(String word) protected String handleDirection(String word) { Bidi bidi = new Bidi(word, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); // if there is pure LTR text no need to process further if (!bidi.isMixed() && bidi.getBaseLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT) { return word; } // collect individual bidi information int runCount = bidi.getRunCount(); byte[] levels = new byte[runCount]; Integer[] runs = new Integer[runCount]; for (int i = 0; i < runCount; i++) { levels[i] = (byte) bidi.getRunLevel(i); runs[i] = i; } // reorder individual parts based on their levels Bidi.reorderVisually(levels, 0, runs, 0, runCount); // collect the parts based on the direction within the run StringBuilder result = new StringBuilder(); for (int i = 0; i < runCount; i++) { int index = runs[i]; int start = bidi.getRunStart(index); int end = bidi.getRunLimit(index); int level = levels[index]; if ((level & 1) != 0) { for (; --end >= start;) { char character = word.charAt(end); if (Character.isMirrored(word.codePointAt(end))) { if (MIRRORING_CHAR_MAP.containsKey(character)) { result.append(MIRRORING_CHAR_MAP.get(character)); } else { result.append(character); } } else { result.append(character); } } } else { result.append(word, start, end); } } return result.toString(); }
From source file:org.apache.pdfbox.text.PDFTextStripper.java
/** * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given * word. If the word is a full line, the results will be the best. If the word contains of single words or * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and * characters!/*from w w w .j a v a2 s. c o m*/ * * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx * * @param word The word that shall be processed * @return new word with the correct direction of the containing characters */ private String handleDirection(String word) { Bidi bidi = new Bidi(word, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); // if there is pure LTR text no need to process further if (!bidi.isMixed() && bidi.getBaseLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT) { return word; } // collect individual bidi information int runCount = bidi.getRunCount(); byte[] levels = new byte[runCount]; Integer[] runs = new Integer[runCount]; for (int i = 0; i < runCount; i++) { levels[i] = (byte) bidi.getRunLevel(i); runs[i] = i; } // reorder individual parts based on their levels Bidi.reorderVisually(levels, 0, runs, 0, runCount); // collect the parts based on the direction within the run StringBuilder result = new StringBuilder(); for (int i = 0; i < runCount; i++) { int index = runs[i]; int start = bidi.getRunStart(index); int end = bidi.getRunLimit(index); int level = levels[index]; if ((level & 1) != 0) { while (--end >= start) { char character = word.charAt(end); if (Character.isMirrored(word.codePointAt(end))) { if (MIRRORING_CHAR_MAP.containsKey(character)) { result.append(MIRRORING_CHAR_MAP.get(character)); } else { result.append(character); } } else { result.append(character); } } } else { result.append(word, start, end); } } return result.toString(); }
From source file:org.openstreetmap.josm.tools.Utils.java
/** * Convert a string to a list of {@link GlyphVector}s. The string may contain * bi-directional text. The result will be in correct visual order. * Each element of the resulting list corresponds to one section of the * string with consistent writing direction (left-to-right or right-to-left). * * @param string the string to render//from w ww. j ava 2 s.c om * @param font the font * @param frc a FontRenderContext object * @return a list of GlyphVectors */ public static List<GlyphVector> getGlyphVectorsBidi(String string, Font font, FontRenderContext frc) { List<GlyphVector> gvs = new ArrayList<>(); Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); byte[] levels = new byte[bidi.getRunCount()]; DirectionString[] dirStrings = new DirectionString[levels.length]; for (int i = 0; i < levels.length; ++i) { levels[i] = (byte) bidi.getRunLevel(i); String substr = string.substring(bidi.getRunStart(i), bidi.getRunLimit(i)); int dir = levels[i] % 2 == 0 ? Bidi.DIRECTION_LEFT_TO_RIGHT : Bidi.DIRECTION_RIGHT_TO_LEFT; dirStrings[i] = new DirectionString(dir, substr); } Bidi.reorderVisually(levels, 0, dirStrings, 0, levels.length); for (int i = 0; i < dirStrings.length; ++i) { char[] chars = dirStrings[i].str.toCharArray(); gvs.add(font.layoutGlyphVector(frc, chars, 0, chars.length, dirStrings[i].direction)); } return gvs; }