Android String Width Get displayWidth(String s)

Here you can find the source of displayWidth(String s)

Description

Returns the approximate display width of the string, measured in units of ascii characters.

License

Apache License

Declaration

public static int displayWidth(String s) 

Method Source Code

//package com.java2s;
/**// ww  w  .  j ava 2 s.  co m
 * Copyright (c) 2000, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns the approximate display width of the string, measured in units of
     * ascii characters.
     *
     * @see StringUtil#displayWidth(char)
     */
    public static int displayWidth(String s) {
        // TODO(kevinb): could reimplement this as
        // return s.length() * 2 - CharMatcher.SINGLE_WIDTH.countIn(s);
        int width = 0;
        int len = s.length();
        for (int i = 0; i < len; ++i) {
            width += displayWidth(s.charAt(i));
        }
        return width;
    }

    /**
     * Returns the approximate display width of the character, measured
     * in units of ascii characters.
     *
     * This method should err on the side of caution. By default, characters
     * are assumed to have width 2; this covers CJK ideographs, various
     * symbols and miscellaneous weird scripts. Given below are some Unicode
     * ranges for which it seems safe to assume that no character is
     * substantially wider than an ascii character:
     *   - Latin, extended Latin, even more extended Latin.
     *   - Greek, extended Greek, Cyrillic.
     *   - Some symbols (including currency symbols) and punctuation.
     *   - Half-width Katakana and Hangul.
     *   - Hebrew
     *   - Arabic
     *   - Thai
     * Characters in these ranges are given a width of 1.
     *
     * IMPORTANT: this function has analogs in C++ (encodingutils.cc,
     * named UnicodeCharWidth) and JavaScript
     * (java/com/google/ads/common/frontend/adwordsbase/resources/CreateAdUtil.js),
     * which need to be updated if you change the implementation here.
     */
    public static int displayWidth(char ch) {
        if (ch <= '\u04f9' || // CYRILLIC SMALL LETTER YERU WITH DIAERESIS
                ch == '\u05be' || // HEBREW PUNCTUATION MAQAF
                (ch >= '\u05d0' && ch <= '\u05ea') || // HEBREW LETTER ALEF ... TAV
                ch == '\u05F3' || // HEBREW PUNCTUATION GERESH
                ch == '\u05f4' || // HEBREW PUNCTUATION GERSHAYIM
                (ch >= '\u0600' && ch <= '\u06ff') || // Block=Arabic
                (ch >= '\u0750' && ch <= '\u077f') || // Block=Arabic_Supplement
                (ch >= '\ufb50' && ch <= '\ufdff') || // Block=Arabic_Presentation_Forms-A
                (ch >= '\ufe70' && ch <= '\ufeff') || // Block=Arabic_Presentation_Forms-B
                (ch >= '\u1e00' && ch <= '\u20af') || /* LATIN CAPITAL LETTER A WITH RING BELOW
                                                         ... DRACHMA SIGN */
                (ch >= '\u2100' && ch <= '\u213a') || // ACCOUNT OF ... ROTATED CAPITAL Q
                (ch >= '\u0e00' && ch <= '\u0e7f') || // Thai
                (ch >= '\uff61' && ch <= '\uffdc')) { /* HALFWIDTH IDEOGRAPHIC FULL STOP
                                                         ... HALFWIDTH HANGUL LETTER I */
            return 1;
        }
        return 2;
    }
}

Related

  1. getStringsWidth(TextPaint paint, String text)