14ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta/*
24ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
34ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta *
44ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
54ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * you may not use this file except in compliance with the License.
64ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * You may obtain a copy of the License at
74ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta *
84ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
94ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta *
104ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
114ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
124ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * See the License for the specific language governing permissions and
144ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta * limitations under the License.
154ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta */
164ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta
174ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Guptapackage android.text;
184ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta
19442aee6bc1abfb143dcfa1ba60d696e576d066c4Deepanshu Guptaimport android.annotation.Nullable;
204ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta
214ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta// Based on the native implementation of TabStops in
224ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta// frameworks/base/core/jni/android_text_StaticLayout.cpp revision b808260
234ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Guptapublic class TabStops {
244ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    @Nullable
254ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    private int[] mStops;
264ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    private final int mTabWidth;
274ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta
284ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    public TabStops(@Nullable int[] stops, int defaultTabWidth) {
294ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        mTabWidth = defaultTabWidth;
304ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        mStops = stops;
314ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    }
324ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta
334ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    public float width(float widthSoFar) {
344ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        if (mStops != null) {
354ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta            for (int i : mStops) {
364ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta                if (i > widthSoFar) {
374ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta                    return i;
384ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta                }
394ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta            }
404ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        }
414ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        // find the next tabStop after widthSoFar.
424ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta        return (int) ((widthSoFar + mTabWidth) / mTabWidth) * mTabWidth;
434ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta    }
444ef9b507c2c1b73805533a86d935d637493d5903Deepanshu Gupta}
45