a611618d731dd9ed82e63b1740e32243450cc41d
[jvcard.git] / CJKUtils.java
1 /*
2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
3 *
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright (C) 2010-2015 Martin
18 */
19 package com.googlecode.lanterna;
20
21 /**
22 * Utilities class for analyzing and working with CJK (Chinese, Japanese, Korean) characters. The main purpose of this
23 * class is to assist in figuring out how many terminal columns a character (and in extension, a String) takes up. The
24 * main issue is that while most latin (and latin-related) character can be trusted to consume one column in the
25 * terminal, CJK characters tends to take two, partly due to the square nature of the characters but mostly due to the
26 * fact that they require most space to distinguish.
27 *
28 * @author Martin
29 * @see TerminalTextUtils
30 * @deprecated Use {@code TerminalTextUtils} instead
31 */
32 public class CJKUtils {
33 private CJKUtils() {
34 }
35
36 /**
37 * Given a character, is this character considered to be a CJK character?
38 * Shamelessly stolen from
39 * <a href="http://stackoverflow.com/questions/1499804/how-can-i-detect-japanese-text-in-a-java-string">StackOverflow</a>
40 * where it was contributed by user Rakesh N
41 * @param c Character to test
42 * @return {@code true} if the character is a CJK character
43 * @deprecated Use {@code TerminalTextUtils.isCharJCK(c)} instead
44 * @see TerminalTextUtils#isCharCJK(char)
45 */
46 @Deprecated
47 public static boolean isCharCJK(final char c) {
48 return TerminalTextUtils.isCharCJK(c);
49 }
50
51 /**
52 * @deprecated Call {@code getColumnWidth(s)} instead
53 */
54 @Deprecated
55 public static int getTrueWidth(String s) {
56 return TerminalTextUtils.getColumnWidth(s);
57 }
58
59 /**
60 * Given a string, returns how many columns this string would need to occupy in a terminal, taking into account that
61 * CJK characters takes up two columns.
62 * @param s String to check length
63 * @return Number of actual terminal columns the string would occupy
64 * @deprecated Use {@code TerminalTextUtils.getColumnWidth(s)} instead
65 * @see TerminalTextUtils#getColumnWidth(String)
66 */
67 @Deprecated
68 public static int getColumnWidth(String s) {
69 return TerminalTextUtils.getColumnIndex(s, s.length());
70 }
71
72 /**
73 * Given a string and a character index inside that string, find out what the column index of that character would
74 * be if printed in a terminal. If the string only contains non-CJK characters then the returned value will be same
75 * as {@code stringCharacterIndex}, but if there are CJK characters the value will be different due to CJK
76 * characters taking up two columns in width. If the character at the index in the string is a CJK character itself,
77 * the returned value will be the index of the left-side of character.
78 * @param s String to translate the index from
79 * @param stringCharacterIndex Index within the string to get the terminal column index of
80 * @return Index of the character inside the String at {@code stringCharacterIndex} when it has been writted to a
81 * terminal
82 * @throws StringIndexOutOfBoundsException if the index given is outside the String length or negative
83 * @deprecated Use {@code TerminalTextUtils.getColumnIndex(s, stringCharacterIndex)} instead
84 * @see TerminalTextUtils#getColumnIndex(String, int)
85 */
86 @Deprecated
87 public static int getColumnIndex(String s, int stringCharacterIndex) throws StringIndexOutOfBoundsException {
88 return TerminalTextUtils.getColumnIndex(s, stringCharacterIndex);
89 }
90
91 /**
92 * This method does the reverse of getColumnIndex, given a String and imagining it has been printed out to the
93 * top-left corner of a terminal, in the column specified by {@code columnIndex}, what is the index of that
94 * character in the string. If the string contains no CJK characters, this will always be the same as
95 * {@code columnIndex}. If the index specified is the right column of a CJK character, the index is the same as if
96 * the column was the left column. So calling {@code getStringCharacterIndex("英", 0)} and
97 * {@code getStringCharacterIndex("英", 1)} will both return 0.
98 * @param s String to translate the index to
99 * @param columnIndex Column index of the string written to a terminal
100 * @return The index in the string of the character in terminal column {@code columnIndex}
101 * @deprecated Use {@code TerminalTextUtils.getStringCharacterIndex(s, columnIndex} instead
102 * @see TerminalTextUtils#getStringCharacterIndex(String, int)
103 */
104 @Deprecated
105 public static int getStringCharacterIndex(String s, int columnIndex) {
106 return TerminalTextUtils.getStringCharacterIndex(s, columnIndex);
107 }
108
109 /**
110 * Given a string that may or may not contain CJK characters, returns the substring which will fit inside
111 * <code>availableColumnSpace</code> columns. This method does not handle special cases like tab or new-line.
112 * <p>
113 * Calling this method is the same as calling {@code fitString(string, 0, availableColumnSpace)}.
114 * @param string The string to fit inside the availableColumnSpace
115 * @param availableColumnSpace Number of columns to fit the string inside
116 * @return The whole or part of the input string which will fit inside the supplied availableColumnSpace
117 * @deprecated Use {@code TerminalTextUtils.fitString(string, availableColumnSpace)} instead
118 * @see TerminalTextUtils#fitString(String, int)
119 */
120 @Deprecated
121 public static String fitString(String string, int availableColumnSpace) {
122 return TerminalTextUtils.fitString(string, availableColumnSpace);
123 }
124
125 /**
126 * Given a string that may or may not contain CJK characters, returns the substring which will fit inside
127 * <code>availableColumnSpace</code> columns. This method does not handle special cases like tab or new-line.
128 * <p>
129 * This overload has a {@code fromColumn} parameter that specified where inside the string to start fitting. Please
130 * notice that {@code fromColumn} is not a character index inside the string, but a column index as if the string
131 * has been printed from the left-most side of the terminal. So if the string is "日本語", fromColumn set to 1 will
132 * not starting counting from the second character ("本") in the string but from the CJK filler character belonging
133 * to "日". If you want to count from a particular character index inside the string, please pass in a substring
134 * and use fromColumn set to 0.
135 * @param string The string to fit inside the availableColumnSpace
136 * @param fromColumn From what column of the input string to start fitting (see description above!)
137 * @param availableColumnSpace Number of columns to fit the string inside
138 * @return The whole or part of the input string which will fit inside the supplied availableColumnSpace
139 * @deprecated Use {@code TerminalTextUtils.fitString(string, fromColumn, availableColumnSpace)} instead
140 * @see TerminalTextUtils#fitString(String, int, int)
141 */
142 @Deprecated
143 public static String fitString(String string, int fromColumn, int availableColumnSpace) {
144 return TerminalTextUtils.fitString(string, fromColumn, availableColumnSpace);
145 }
146 }