Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0aa241d3c32a1be4673cf9dbfa414b1dad385708 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*******************************************************************************
 * Copyright (c) 2010, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.equinox.bidi.internal.consumable;

import org.eclipse.equinox.bidi.STextDirection;
import org.eclipse.equinox.bidi.advanced.STextEnvironment;
import org.eclipse.equinox.bidi.custom.STextCharTypes;
import org.eclipse.equinox.bidi.custom.STextProcessor;

/**
 *  Processor adapted to processing arithmetic expressions with
 *  a possible right-to-left base direction.
 */
public class STextMath extends STextProcessor {
	static final byte L = Character.DIRECTIONALITY_LEFT_TO_RIGHT;
	static final byte R = Character.DIRECTIONALITY_RIGHT_TO_LEFT;
	static final byte AL = Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
	static final byte AN = Character.DIRECTIONALITY_ARABIC_NUMBER;

	public STextMath() {
		super("+-/*()="); //$NON-NLS-1$
	}

	public int getDirection(STextEnvironment environment, String text) {
		return getDirection(environment, text, new STextCharTypes(this, environment, text));
	}

	/**
	 *  @return {@link STextDirection#DIR_RTL DIR_RTL} if the following
	 *          conditions are satisfied:
	 *          <ul>
	 *            <li>The current locale (as expressed by the environment
	 *                language) is Arabic.</li>
	 *            <li>The first strong character is an Arabic letter.</li>
	 *            <li>If there is no strong character in the text, there is
	 *                at least one Arabic-Indic digit in the text.</li>
	 *          </ul>
	 *          Otherwise, returns {@link STextDirection#DIR_LTR DIR_LTR}.
	 */
	public int getDirection(STextEnvironment environment, String text, STextCharTypes charTypes) {
		String language = environment.getLanguage();
		if (!language.equals("ar")) //$NON-NLS-1$
			return STextDirection.DIR_LTR;
		boolean flagAN = false;
		for (int i = 0; i < text.length(); i++) {
			byte charType = charTypes.getBidiTypeAt(i);
			if (charType == AL)
				return STextDirection.DIR_RTL;
			if (charType == L || charType == R)
				return STextDirection.DIR_LTR;
			if (charType == AN)
				flagAN = true;
		}
		if (flagAN)
			return STextDirection.DIR_RTL;
		return STextDirection.DIR_LTR;
	}
}

Back to the top