Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.text;
     18 
     19 import com.android.ide.common.rendering.api.LayoutLog;
     20 import com.android.layoutlib.bridge.Bridge;
     21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
     22 import com.ibm.icu.text.Bidi;
     23 
     24 
     25 /**
     26  * Delegate used to provide new implementation for the native methods of {@link AndroidBidi}
     27  *
     28  * Through the layoutlib_create tool, the original  methods of AndroidBidi have been replaced
     29  * by calls to methods of the same name in this delegate class.
     30  *
     31  */
     32 public class AndroidBidi_Delegate {
     33 
     34     @LayoutlibDelegate
     35     /*package*/ static int runBidi(int dir, char[] chars, byte[] charInfo, int count,
     36             boolean haveInfo) {
     37 
     38         switch (dir) {
     39         case 0: // Layout.DIR_REQUEST_LTR
     40             dir = Bidi.LTR;
     41             break;
     42         case 1: // Layout.DIR_REQUEST_RTL
     43             dir = Bidi.RTL;
     44             break;
     45         case -1: // Layout.DIR_REQUEST_DEFAULT_RTL
     46             dir = Bidi.LEVEL_DEFAULT_RTL;
     47             break;
     48         case -2: // Layout.DIR_REQUEST_DEFAULT_LTR
     49             dir = Bidi.LEVEL_DEFAULT_LTR;
     50             break;
     51         default:
     52             // Invalid code. Log error, assume LEVEL_DEFAULT_LTR and continue.
     53             Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Invalid direction flag", null);
     54             dir = Bidi.LEVEL_DEFAULT_LTR;
     55         }
     56         Bidi bidi = new Bidi(chars, 0, null, 0, count, dir);
     57         if (charInfo != null) {
     58             for (int i = 0; i < count; ++i)
     59             charInfo[i] = bidi.getLevelAt(i);
     60         }
     61         return bidi.getParaLevel();
     62     }
     63 }
     64