You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CharUtilities.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. /**
  20. * This class provides utilities to distinguish various kinds of Unicode
  21. * whitespace and to get character widths in a given FontState.
  22. */
  23. public class CharUtilities {
  24. /**
  25. * Character code used to signal a character boundary in
  26. * inline content, such as an inline with borders and padding
  27. * or a nested block object.
  28. */
  29. public static final char CODE_EOT = 0;
  30. /**
  31. * Character class: Unicode white space
  32. */
  33. public static final int UCWHITESPACE = 0;
  34. /**
  35. * Character class: Line feed
  36. */
  37. public static final int LINEFEED = 1;
  38. /**
  39. * Character class: Boundary between text runs
  40. */
  41. public static final int EOT = 2;
  42. /**
  43. * Character class: non-whitespace
  44. */
  45. public static final int NONWHITESPACE = 3;
  46. /**
  47. * Character class: XML whitespace
  48. */
  49. public static final int XMLWHITESPACE = 4;
  50. /** null char */
  51. public static final char NULL_CHAR = '\u0000';
  52. /** linefeed character */
  53. public static final char LINEFEED_CHAR = '\n';
  54. /** carriage return */
  55. public static final char CARRIAGE_RETURN = '\r';
  56. /** normal tab */
  57. public static final char TAB = '\t';
  58. /** normal space */
  59. public static final char SPACE = '\u0020';
  60. /** non-breaking space */
  61. public static final char NBSPACE = '\u00A0';
  62. /** next line control character */
  63. public static final char NEXT_LINE = '\u0085';
  64. /** zero-width space */
  65. public static final char ZERO_WIDTH_SPACE = '\u200B';
  66. /** word joiner */
  67. public static final char WORD_JOINER = '\u2060';
  68. /** zero-width joiner */
  69. public static final char ZERO_WIDTH_JOINER = '\u200D';
  70. /** zero-width no-break space (= byte order mark) */
  71. public static final char ZERO_WIDTH_NOBREAK_SPACE = '\uFEFF';
  72. /** soft hyphen */
  73. public static final char SOFT_HYPHEN = '\u00AD';
  74. /** line-separator */
  75. public static final char LINE_SEPARATOR = '\u2028';
  76. /** paragraph-separator */
  77. public static final char PARAGRAPH_SEPARATOR = '\u2029';
  78. /** missing ideograph */
  79. public static final char MISSING_IDEOGRAPH = '\u25A1';
  80. /** Ideogreaphic space */
  81. public static final char IDEOGRAPHIC_SPACE = '\u3000';
  82. /** Unicode value indicating the the character is "not a character". */
  83. public static final char NOT_A_CHARACTER = '\uFFFF';
  84. /**
  85. * Utility class: Constructor prevents instantiating when subclassed.
  86. */
  87. protected CharUtilities() {
  88. throw new UnsupportedOperationException();
  89. }
  90. /**
  91. * Return the appropriate CharClass constant for the type
  92. * of the passed character.
  93. * @param c character to inspect
  94. * @return the determined character class
  95. */
  96. public static int classOf(char c) {
  97. switch (c) {
  98. case CODE_EOT:
  99. return EOT;
  100. case LINEFEED_CHAR:
  101. return LINEFEED;
  102. case SPACE:
  103. case CARRIAGE_RETURN:
  104. case TAB:
  105. return XMLWHITESPACE;
  106. default:
  107. return isAnySpace(c) ? UCWHITESPACE : NONWHITESPACE;
  108. }
  109. }
  110. /**
  111. * Helper method to determine if the character is a
  112. * space with normal behavior. Normal behavior means that
  113. * it's not non-breaking.
  114. * @param c character to inspect
  115. * @return True if the character is a normal space
  116. */
  117. public static boolean isBreakableSpace(char c) {
  118. return (c == SPACE || isFixedWidthSpace(c));
  119. }
  120. /**
  121. * Method to determine if the character is a zero-width space.
  122. * @param c the character to check
  123. * @return true if the character is a zero-width space
  124. */
  125. public static boolean isZeroWidthSpace(char c) {
  126. return c == ZERO_WIDTH_SPACE // 200Bh
  127. || c == WORD_JOINER // 2060h
  128. || c == ZERO_WIDTH_NOBREAK_SPACE; // FEFFh (also used as BOM)
  129. }
  130. /**
  131. * Method to determine if the character is a (breakable) fixed-width space.
  132. * @param c the character to check
  133. * @return true if the character has a fixed-width
  134. */
  135. public static boolean isFixedWidthSpace(char c) {
  136. return (c >= '\u2000' && c <= '\u200B')
  137. || c == '\u3000';
  138. // c == '\u2000' // en quad
  139. // c == '\u2001' // em quad
  140. // c == '\u2002' // en space
  141. // c == '\u2003' // em space
  142. // c == '\u2004' // three-per-em space
  143. // c == '\u2005' // four-per-em space
  144. // c == '\u2006' // six-per-em space
  145. // c == '\u2007' // figure space
  146. // c == '\u2008' // punctuation space
  147. // c == '\u2009' // thin space
  148. // c == '\u200A' // hair space
  149. // c == '\u200B' // zero width space
  150. // c == '\u3000' // ideographic space
  151. }
  152. /**
  153. * Method to determine if the character is a nonbreaking
  154. * space.
  155. * @param c character to check
  156. * @return True if the character is a nbsp
  157. */
  158. public static boolean isNonBreakableSpace(char c) {
  159. return
  160. (c == NBSPACE // no-break space
  161. || c == '\u202F' // narrow no-break space
  162. || c == '\u3000' // ideographic space
  163. || c == WORD_JOINER // word joiner
  164. || c == ZERO_WIDTH_NOBREAK_SPACE); // zero width no-break space
  165. }
  166. /**
  167. * Method to determine if the character is an adjustable
  168. * space.
  169. * @param c character to check
  170. * @return True if the character is adjustable
  171. */
  172. public static boolean isAdjustableSpace(char c) {
  173. //TODO: are there other kinds of adjustable spaces?
  174. return
  175. (c == '\u0020' // normal space
  176. || c == NBSPACE); // no-break space
  177. }
  178. /**
  179. * Determines if the character represents any kind of space.
  180. * @param c character to check
  181. * @return True if the character represents any kind of space
  182. */
  183. public static boolean isAnySpace(char c) {
  184. return (isBreakableSpace(c) || isNonBreakableSpace(c));
  185. }
  186. /**
  187. * Indicates whether a character is classified as "Alphabetic" by the Unicode standard.
  188. * @param ch the character
  189. * @return true if the character is "Alphabetic"
  190. */
  191. public static boolean isAlphabetic(char ch) {
  192. //http://www.unicode.org/Public/UNIDATA/UCD.html#Alphabetic
  193. //Generated from: Other_Alphabetic + Lu + Ll + Lt + Lm + Lo + Nl
  194. int generalCategory = Character.getType(ch);
  195. switch (generalCategory) {
  196. case Character.UPPERCASE_LETTER: //Lu
  197. case Character.LOWERCASE_LETTER: //Ll
  198. case Character.TITLECASE_LETTER: //Lt
  199. case Character.MODIFIER_LETTER: //Lm
  200. case Character.OTHER_LETTER: //Lo
  201. case Character.LETTER_NUMBER: //Nl
  202. return true;
  203. default:
  204. //TODO if (ch in Other_Alphabetic) return true; (Probably need ICU4J for that)
  205. //Other_Alphabetic contains mostly more exotic characters
  206. return false;
  207. }
  208. }
  209. /**
  210. * Indicates whether the given character is an explicit break-character
  211. * @param ch the character to check
  212. * @return true if the character represents an explicit break
  213. */
  214. public static boolean isExplicitBreak(char ch) {
  215. return (ch == LINEFEED_CHAR
  216. || ch == CARRIAGE_RETURN
  217. || ch == NEXT_LINE
  218. || ch == LINE_SEPARATOR
  219. || ch == PARAGRAPH_SEPARATOR);
  220. }
  221. }