1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  ******************************************************************************/
14 package org.eclipse.equinox.bidi.internal.tests;
15 
16 import org.eclipse.equinox.bidi.advanced.IStructuredTextExpert;
17 import org.eclipse.equinox.bidi.custom.StructuredTextCharTypes;
18 import org.eclipse.equinox.bidi.custom.StructuredTextTypeHandler;
19 
20 /**
21  *  Handler adapted to processing arithmetic expressions with
22  *  a possible right-to-left base direction.
23  */
24 public class StructuredTextMath extends StructuredTextTypeHandler {
25 	static final byte L = Character.DIRECTIONALITY_LEFT_TO_RIGHT;
26 	static final byte R = Character.DIRECTIONALITY_RIGHT_TO_LEFT;
27 	static final byte AL = Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
28 	static final byte AN = Character.DIRECTIONALITY_ARABIC_NUMBER;
29 
StructuredTextMath()30 	public StructuredTextMath() {
31 		super("+-/*()="); //$NON-NLS-1$
32 	}
33 
getDirection(IStructuredTextExpert expert, String text)34 	public int getDirection(IStructuredTextExpert expert, String text) {
35 		return getDirection(expert, text, new StructuredTextCharTypes(expert, text));
36 	}
37 
38 	/**
39 	 *  @return {@link IStructuredTextExpert#DIR_RTL DIR_RTL} if the following
40 	 *          conditions are satisfied:
41 	 *          <ul>
42 	 *            <li>The current locale (as expressed by the environment
43 	 *                language) is Arabic.</li>
44 	 *            <li>The first strong character is an Arabic letter.</li>
45 	 *            <li>If there is no strong character in the text, there is
46 	 *                at least one Arabic-Indic digit in the text.</li>
47 	 *          </ul>
48 	 *          Otherwise, returns {@link IStructuredTextExpert#DIR_LTR DIR_LTR}.
49 	 */
getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes)50 	public int getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes) {
51 		String language = expert.getEnvironment().getLanguage();
52 		if (!language.equals("ar")) //$NON-NLS-1$
53 			return IStructuredTextExpert.DIR_LTR;
54 		boolean flagAN = false;
55 		for (int i = 0; i < text.length(); i++) {
56 			byte charType = charTypes.getBidiTypeAt(i);
57 			if (charType == AL)
58 				return IStructuredTextExpert.DIR_RTL;
59 			if (charType == L || charType == R)
60 				return IStructuredTextExpert.DIR_LTR;
61 			if (charType == AN)
62 				flagAN = true;
63 		}
64 		if (flagAN)
65 			return IStructuredTextExpert.DIR_RTL;
66 		return IStructuredTextExpert.DIR_LTR;
67 	}
68 }
69