選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CharUtilities.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  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. /* $Id$ */
  17. package org.apache.fop.util;
  18. /**
  19. * This class provides utilities to distinguish various kinds of Unicode
  20. * whitespace and to get character widths in a given FontState.
  21. */
  22. public class CharUtilities {
  23. /**
  24. * Character code used to signal a character boundary in
  25. * inline content, such as an inline with borders and padding
  26. * or a nested block object.
  27. */
  28. public static final char CODE_EOT = 0;
  29. /**
  30. * Character class: Unicode white space
  31. */
  32. public static final int UCWHITESPACE = 0;
  33. /**
  34. * Character class: Line feed
  35. */
  36. public static final int LINEFEED = 1;
  37. /**
  38. * Character class: Boundary between text runs
  39. */
  40. public static final int EOT = 2;
  41. /**
  42. * Character class: non-whitespace
  43. */
  44. public static final int NONWHITESPACE = 3;
  45. /**
  46. * Character class: XML whitespace
  47. */
  48. public static final int XMLWHITESPACE = 4;
  49. /**
  50. * Utility class: Constructor prevents instantiating when subclassed.
  51. */
  52. protected CharUtilities() {
  53. throw new UnsupportedOperationException();
  54. }
  55. /**
  56. * Return the appropriate CharClass constant for the type
  57. * of the passed character.
  58. * @param c character to inspect
  59. * @return the determined character class
  60. */
  61. public static int classOf(char c) {
  62. if (c == CODE_EOT) { return EOT; }
  63. if (c == '\n') { return LINEFEED; }
  64. if (c == ' ' || c == '\r' || c == '\t') { return XMLWHITESPACE; }
  65. if (isAnySpace(c)) { return UCWHITESPACE; }
  66. return NONWHITESPACE;
  67. }
  68. /**
  69. * Helper method to determine if the character is a
  70. * space with normal behavior. Normal behavior means that
  71. * it's not non-breaking.
  72. * @param c character to inspect
  73. * @return True if the character is a normal space
  74. */
  75. public static boolean isBreakableSpace(char c) {
  76. return (c == ' '
  77. || (c >= '\u2000' && c <= '\u200B'));
  78. // c == '\u2000' // en quad
  79. // c == '\u2001' // em quad
  80. // c == '\u2002' // en space
  81. // c == '\u2003' // em space
  82. // c == '\u2004' // three-per-em space
  83. // c == '\u2005' // four--per-em space
  84. // c == '\u2006' // six-per-em space
  85. // c == '\u2007' // figure space
  86. // c == '\u2008' // punctuation space
  87. // c == '\u2009' // thin space
  88. // c == '\u200A' // hair space
  89. // c == '\u200B' // zero width space
  90. }
  91. /**
  92. * Method to determine if the character is a nonbreaking
  93. * space.
  94. * @param c character to check
  95. * @return True if the character is a nbsp
  96. */
  97. public static boolean isNonBreakableSpace(char c) {
  98. return
  99. (c == '\u00A0' // no-break space
  100. || c == '\u202F' // narrow no-break space
  101. || c == '\u3000' // ideographic space
  102. || c == '\uFEFF'); // zero width no-break space
  103. }
  104. /**
  105. * Determines if the character represents any kind of space.
  106. * @param c character to check
  107. * @return True if the character represents any kind of space
  108. */
  109. public static boolean isAnySpace(char c) {
  110. boolean ret = (isBreakableSpace(c) || isNonBreakableSpace(c));
  111. return ret;
  112. }
  113. }