Example usage for android.text TextDirectionHeuristic isRtl

List of usage examples for android.text TextDirectionHeuristic isRtl

Introduction

In this page you can find the example usage for android.text TextDirectionHeuristic isRtl.

Prototype

boolean isRtl(CharSequence cs, int start, int count);

Source Link

Document

Guess if a CharSequence is in the RTL direction or not.

Usage

From source file:android.text.BidiFormatter.java

/**
 * Returns a Unicode bidi mark matching the context directionality (LRM or RLM) if either the
 * overall or the exit directionality of a given string is opposite to the context directionality.
 * Putting this after the string (including its directionality declaration wrapping) prevents it
 * from "sticking" to other opposite-directionality text or a number appearing after it inline
 * with only neutral content in between. Otherwise returns the empty string. While the exit
 * directionality is determined by scanning the end of the string, the overall directionality is
 * given explicitly by a heuristic to estimate the {@code str}'s directionality.
 *
 * @param str String after which the mark may need to appear.
 * @param heuristic The text direction heuristic that will be used to estimate the {@code str}'s
 *                  directionality.//from   www .  jav  a  2  s. co  m
 * @return LRM for RTL text in LTR context; RLM for LTR text in RTL context;
 *     else, the empty string.
 *
 * @hide
 */
public String markAfter(String str, TextDirectionHeuristic heuristic) {
    final boolean isRtl = heuristic.isRtl(str, 0, str.length());
    // getExitDir() is called only if needed (short-circuit).
    if (!mIsRtlContext && (isRtl || getExitDir(str) == DIR_RTL)) {
        return LRM_STRING;
    }
    if (mIsRtlContext && (!isRtl || getExitDir(str) == DIR_LTR)) {
        return RLM_STRING;
    }
    return EMPTY_STRING;
}

From source file:android.text.BidiFormatter.java

/**
 * Returns a Unicode bidi mark matching the context directionality (LRM or RLM) if either the
 * overall or the entry directionality of a given string is opposite to the context
 * directionality. Putting this before the string (including its directionality declaration
 * wrapping) prevents it from "sticking" to other opposite-directionality text appearing before
 * it inline with only neutral content in between. Otherwise returns the empty string. While the
 * entry directionality is determined by scanning the beginning of the string, the overall
 * directionality is given explicitly by a heuristic to estimate the {@code str}'s directionality.
 *
 * @param str String before which the mark may need to appear.
 * @param heuristic The text direction heuristic that will be used to estimate the {@code str}'s
 *                  directionality./*from   ww  w. jav a2  s  .  c o  m*/
 * @return LRM for RTL text in LTR context; RLM for LTR text in RTL context;
 *     else, the empty string.
 *
 * @hide
 */
public String markBefore(String str, TextDirectionHeuristic heuristic) {
    final boolean isRtl = heuristic.isRtl(str, 0, str.length());
    // getEntryDir() is called only if needed (short-circuit).
    if (!mIsRtlContext && (isRtl || getEntryDir(str) == DIR_RTL)) {
        return LRM_STRING;
    }
    if (mIsRtlContext && (!isRtl || getEntryDir(str) == DIR_LTR)) {
        return RLM_STRING;
    }
    return EMPTY_STRING;
}

From source file:android.text.BidiFormatter.java

/**
 * Formats a string of given directionality for use in plain-text output of the context
 * directionality, so an opposite-directionality string is neither garbled nor garbles its
 * surroundings. This makes use of Unicode bidi formatting characters.
 * <p>//from  w w w  .j  ava2  s  . c o m
 * The algorithm: In case the given directionality doesn't match the context directionality, wraps
 * the string with Unicode bidi formatting characters: RLE+{@code str}+PDF for RTL text, or
 * LRE+{@code str}+PDF for LTR text.
 * <p>
 * If {@code isolate}, directionally isolates the string so that it does not garble its
 * surroundings. Currently, this is done by "resetting" the directionality after the string by
 * appending a trailing Unicode bidi mark matching the context directionality (LRM or RLM) when
 * either the overall directionality or the exit directionality of the string is opposite to that
 * of the context. If the formatter was built using {@link Builder#stereoReset(boolean)} and
 * passing "true" as an argument, also prepends a Unicode bidi mark matching the context
 * directionality when either the overall directionality or the entry directionality of the
 * string is opposite to that of the context. Note that as opposed to the overall
 * directionality, the entry and exit directionalities are determined from the string itself.
 * <p>
 * Does *not* do HTML-escaping.
 *
 * @param str The input string.
 * @param heuristic The algorithm to be used to estimate the string's overall direction.
 *        See {@link TextDirectionHeuristics} for pre-defined heuristics.
 * @param isolate Whether to directionally isolate the string to prevent it from garbling the
 *     content around it
 * @return Input string after applying the above processing.
 */
public String unicodeWrap(String str, TextDirectionHeuristic heuristic, boolean isolate) {
    final boolean isRtl = heuristic.isRtl(str, 0, str.length());
    StringBuilder result = new StringBuilder();
    if (getStereoReset() && isolate) {
        result.append(markBefore(str, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR));
    }
    if (isRtl != mIsRtlContext) {
        result.append(isRtl ? RLE : LRE);
        result.append(str);
        result.append(PDF);
    } else {
        result.append(str);
    }
    if (isolate) {
        result.append(markAfter(str, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR));
    }
    return result.toString();
}