本文整理匯總了Java中java.text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT屬性的典型用法代碼示例。如果您正苦於以下問題:Java Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT屬性的具體用法?Java Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT怎麽用?Java Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類java.text.Bidi
的用法示例。
在下文中一共展示了Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT屬性的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isRTL
/**
* This method determines if the direction of a substring is right-to-left.
* If the string is empty that determination is based on the default system language
* Locale.getDefault().
* The method can handle invalid substring definitions (start > end etc.), in which case the
* method returns False.
*
* @return True if the text direction is right-to-left, false otherwise.
*/
public static boolean isRTL(CharSequence s, int start, int end) {
if (s == null || s.length() == 0) {
// empty string --> determine the direction from the default language
return isRTL(Locale.getDefault());
}
if (start == end) {
// if no character is selected we need to expand the selection
start = Math.max(0, --start);
if (start == end) {
end = Math.min(s.length(), ++end);
}
}
try {
Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
return ! bidi.baseIsLeftToRight();
}
catch (IndexOutOfBoundsException e) {
return false;
}
}
示例2: testGetRunStart
public void testGetRunStart() {
bd = new Bidi(new char[] { 's', 's', 's' }, 0, new byte[] { (byte) -7,
(byte) -2, (byte) 3 }, 0, 3,
Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
assertEquals(0, bd.getRunStart(0));
assertEquals(1, bd.getRunStart(1));
assertEquals(2, bd.getRunStart(2));
String LTR = "\u0061\u0062";
String RTL = "\u05DC\u05DD";
String newLine = "\n";
String defText = LTR + newLine + RTL + LTR + RTL;
int[][] expectedRuns = { { 0, 3 }, { 3, 5 }, { 5, 7 }, { 7, 9 }, };
Bidi bi = new Bidi(defText, 0);
final int count = bi.getRunCount();
for (int i = 0; i < count; i++) {
assertEquals(expectedRuns[i][0], bi.getRunStart(i));
}
}
示例3: test1
static void test1() {
String str = "If this text is >" + target + "< the test passed.";
int start = str.indexOf(target);
int limit = start + target.length();
AttributedString astr = new AttributedString(str);
astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
new Integer(-1),
start,
limit);
Bidi bidi = new Bidi(astr.getIterator());
byte[] embs = new byte[str.length() + 3];
for (int i = start + 1; i < limit + 1; ++i) {
embs[i] = -1;
}
Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
throw new Error("Bidi run count incorrect");
}
}
示例4: getDefaultDirection
private int getDefaultDirection(final Element par) {
Object runDirection = null;
if (par != null) {
runDirection =
par.getAttributes().getAttribute(TextAttribute.RUN_DIRECTION);
if (runDirection != null) {
return getDefaultDirection(runDirection);
}
}
runDirection = getProperty(TextAttribute.RUN_DIRECTION);
if (runDirection != null) {
return getDefaultDirection(runDirection);
} else {
return Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
}
}
示例5: layoutBidi
private int layoutBidi(String str) {
final char[] text = str.toCharArray();
/* Avoid performing full bidirectional analysis if text has no "strong" right-to-left characters */
if (!Bidi.requiresBidi(text, 0, text.length))
return layoutFont(text, 0, text.length, Font.LAYOUT_LEFT_TO_RIGHT);
final Bidi bidi = new Bidi(text, 0, null, 0, text.length, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
/* If text is entirely right-to-left, then just lay it out */
if (bidi.isRightToLeft())
return layoutFont(text, 0, text.length, Font.LAYOUT_RIGHT_TO_LEFT);
/* Otherwise text has a mixture of LTR and RLT, and it requires full bidirectional analysis */
final int runCount = bidi.getRunCount();
final byte[] levels = new byte[runCount];
final Integer[] ranges = new Integer[runCount];
for (int i = 0; i < runCount; i++) {
levels[i] = (byte) bidi.getRunLevel(i);
ranges[i] = i;
}
Bidi.reorderVisually(levels, 0, ranges, 0, runCount);
int width = 0;
for (int i = 0; i < runCount; i++) {
final int index = ranges[i];
width += layoutFont(text, bidi.getRunStart(index), bidi.getRunLimit(index), (bidi.getRunLevel(index) & 1));
}
return width;
}
示例6: testBidi
void testBidi(String string, boolean directionIsRTL) {
Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if (bidi.isMixed()) {
throw new RuntimeException("bidi is mixed");
}
if (bidi.isRightToLeft() != directionIsRTL) {
throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
}
}
示例7: isLeftToRight
protected boolean isLeftToRight(char[] chars)
{
boolean leftToRight = true;
if (Bidi.requiresBidi(chars, 0, chars.length))
{
// determining the text direction
// using default LTR as there's no way to have other default in the text
Bidi bidi = new Bidi(chars, 0, null, 0, chars.length, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
leftToRight = bidi.baseIsLeftToRight();
}
return leftToRight;
}
示例8: addRunsNoTabs
private static void addRunsNoTabs(List<BidiRun> runs, char[] text, int start, int end) {
if (start >= end) return;
Bidi bidi = new Bidi(text, start, null, 0, end - start, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
int runCount = bidi.getRunCount();
for (int i = 0; i < runCount; i++) {
addOrMergeRun(runs, new BidiRun((byte)bidi.getRunLevel(i), start + bidi.getRunStart(i), start + bidi.getRunLimit(i)));
}
}
示例9: getBidi
/**
* @param isRightToLeft If {@code true}, uses right-to-left as the default direction.
*/
public Bidi getBidi(boolean isRightToLeft) {
int flags = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
if (isRightToLeft) {
flags = Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT;
}
return new Bidi(text, toff, null, 0, len, flags);
}
示例10: testBidi
void testBidi(String string, int rtlstart, int rtllength) {
Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
for (int i = 0; i < bidi.getRunCount(); ++i) {
if ((bidi.getRunLevel(i) & 1) != 0) {
if (bidi.getRunStart(i) != rtlstart ||
bidi.getRunLimit(i) != rtlstart + rtllength) {
throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
}
break;
}
}
}
示例11: test1
static void test1() {
String target = "BACK WARDS";
String str = "If this text is >" + target + "< the test passed.";
int start = str.indexOf(target);
int limit = start + target.length();
System.out.println("start: " + start + " limit: " + limit);
AttributedString astr = new AttributedString(str);
astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
new Integer(-1),
start,
limit);
Bidi bidi = new Bidi(astr.getIterator());
for (int i = 0; i < bidi.getRunCount(); ++i) {
System.out.println("run " + i +
" from " + bidi.getRunStart(i) +
" to " + bidi.getRunLimit(i) +
" at level " + bidi.getRunLevel(i));
}
System.out.println(bidi);
byte[] embs = new byte[str.length() + 3];
for (int i = start + 1; i < limit + 1; ++i) {
embs[i] = -1;
}
Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
for (int i = 0; i < bidi2.getRunCount(); ++i) {
System.out.println("run " + i +
" from " + bidi2.getRunStart(i) +
" to " + bidi2.getRunLimit(i) +
" at level " + bidi2.getRunLevel(i));
}
System.out.println(bidi2 + "\n");
if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
throw new Error("Bidi run count incorrect");
} else {
System.out.println("test1() passed.\n");
}
}
示例12: layoutBidiString
/**
* Split a string into contiguous LTR or RTL sections by applying the Unicode Bidirectional Algorithm. Calls layoutString()
* for each contiguous run to perform further analysis.
*
* @param glyphList will hold all new Glyph objects allocated by layoutFont()
* @param text the string to lay out
* @param start the offset into text at which to start the layout
* @param limit the (offset + length) at which to stop performing the layout
* @return the total advance (horizontal distance) of this string
*/
private int layoutBidiString(List<Glyph> glyphList, char text[], int start, int limit, ColorCode colors[])
{
int advance = 0;
/* Avoid performing full bidirectional analysis if text has no "strong" right-to-left characters */
if (Bidi.requiresBidi(text, start, limit))
{
/* Note that while requiresBidi() uses start/limit the Bidi constructor uses start/length */
Bidi bidi = new Bidi(text, start, null, 0, limit - start, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
/* If text is entirely right-to-left, then insert an EntryText node for the entire string */
if (bidi.isRightToLeft())
{
return layoutStyle(glyphList, text, start, limit, Font.LAYOUT_RIGHT_TO_LEFT, advance, colors);
}
/* Otherwise text has a mixture of LTR and RLT, and it requires full bidirectional analysis */
else
{
int runCount = bidi.getRunCount();
byte levels[] = new byte[runCount];
Integer ranges[] = new Integer[runCount];
/* Reorder contiguous runs of text into their display order from left to right */
for (int index = 0; index < runCount; index++)
{
levels[index] = (byte) bidi.getRunLevel(index);
ranges[index] = new Integer(index);
}
Bidi.reorderVisually(levels, 0, ranges, 0, runCount);
/*
* Every GlyphVector must be created on a contiguous run of left-to-right or right-to-left text. Keep track of
* the horizontal advance between each run of text, so that the glyphs in each run can be assigned a position relative
* to the start of the entire string and not just relative to that run.
*/
for (int visualIndex = 0; visualIndex < runCount; visualIndex++)
{
int logicalIndex = ranges[visualIndex];
/* An odd numbered level indicates right-to-left ordering */
int layoutFlag = (bidi.getRunLevel(logicalIndex) & 1) == 1 ? Font.LAYOUT_RIGHT_TO_LEFT : Font.LAYOUT_LEFT_TO_RIGHT;
advance = layoutStyle(glyphList, text, start + bidi.getRunStart(logicalIndex), start + bidi.getRunLimit(logicalIndex),
layoutFlag, advance, colors);
}
}
return advance;
}
/* If text is entirely left-to-right, then insert an EntryText node for the entire string */
else
{
return layoutStyle(glyphList, text, start, limit, Font.LAYOUT_LEFT_TO_RIGHT, advance, colors);
}
}
示例13: visualToLogical
/**
* 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!
*
* Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx
*
* @param text The word that shall be processed
* @return new word with the correct direction of the containing characters
*/
public static String visualToLogical(String text)
{
if (!CharUtils.isBlank(text))
{
Bidi bidi = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if (!bidi.isLeftToRight())
{
// 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 = text.charAt(end);
if (Character.isMirrored(text.codePointAt(end))
&& MIRRORING_CHAR_MAP.containsKey(character))
{
result.append(MIRRORING_CHAR_MAP.get(character));
}
else
{
result.append(character);
}
}
}
else
{
result.append(text, start, end);
}
}
return result.toString();
}
}
return text;
}
示例14: layoutBidiString
/**
* Split a string into contiguous LTR or RTL sections by applying the Unicode Bidirectional Algorithm. Calls layoutString()
* for each contiguous run to perform further analysis.
*
* @param glyphList will hold all new Glyph objects allocated by layoutFont()
* @param text the string to lay out
* @param start the offset into text at which to start the layout
* @param limit the (offset + length) at which to stop performing the layout
* @return the total advance (horizontal distance) of this string
*/
private int layoutBidiString(List<Glyph> glyphList, char text[], int start, int limit, ColorCode colors[]) {
int advance = 0;
/* Avoid performing full bidirectional analysis if text has no "strong" right-to-left characters */
if (Bidi.requiresBidi(text, start, limit)) {
/* Note that while requiresBidi() uses start/limit the Bidi constructor uses start/length */
Bidi bidi = new Bidi(text, start, null, 0, limit - start, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
/* If text is entirely right-to-left, then insert an EntryText node for the entire string */
if (bidi.isRightToLeft()) {
return layoutStyle(glyphList, text, start, limit, Font.LAYOUT_RIGHT_TO_LEFT, advance, colors);
}
/* Otherwise text has a mixture of LTR and RLT, and it requires full bidirectional analysis */
else {
int runCount = bidi.getRunCount();
byte levels[] = new byte[runCount];
Integer ranges[] = new Integer[runCount];
/* Reorder contiguous runs of text into their display order from left to right */
for (int index = 0; index < runCount; index++) {
levels[index] = (byte) bidi.getRunLevel(index);
ranges[index] = new Integer(index);
}
Bidi.reorderVisually(levels, 0, ranges, 0, runCount);
/*
* Every GlyphVector must be created on a contiguous run of left-to-right or right-to-left text. Keep track of
* the horizontal advance between each run of text, so that the glyphs in each run can be assigned a position relative
* to the start of the entire string and not just relative to that run.
*/
for (int visualIndex = 0; visualIndex < runCount; visualIndex++) {
int logicalIndex = ranges[visualIndex];
/* An odd numbered level indicates right-to-left ordering */
int layoutFlag = (bidi.getRunLevel(logicalIndex) & 1) == 1 ? Font.LAYOUT_RIGHT_TO_LEFT : Font.LAYOUT_LEFT_TO_RIGHT;
advance = layoutStyle(glyphList, text, start + bidi.getRunStart(logicalIndex), start + bidi.getRunLimit(logicalIndex),
layoutFlag, advance, colors);
}
}
return advance;
}
/* If text is entirely left-to-right, then insert an EntryText node for the entire string */
else {
return layoutStyle(glyphList, text, start, limit, Font.LAYOUT_LEFT_TO_RIGHT, advance, colors);
}
}