一、前言
关于fop的fop-core源码包的org.apache.fop.util.CharUtilities字符处理工具类,区分各种Unicode空格和在给定fontstate得到字符宽度、等值判断、以“0x”格式的需填补留下的“0”处理等
二、源码说明
package org.apache.fop.util;@b@@b@/**@b@ * This class provides utilities to distinguish various kinds of Unicode@b@ * whitespace and to get character widths in a given FontState.@b@ */@b@public class CharUtilities {@b@@b@ /**@b@ * Character code used to signal a character boundary in@b@ * inline content, such as an inline with borders and padding@b@ * or a nested block object.@b@ */@b@ public static final char CODE_EOT = 0;@b@@b@ /**@b@ * Character class: Unicode white space@b@ */@b@ public static final int UCWHITESPACE = 0;@b@ /**@b@ * Character class: Line feed@b@ */@b@ public static final int LINEFEED = 1;@b@ /**@b@ * Character class: Boundary between text runs@b@ */@b@ public static final int EOT = 2;@b@ /**@b@ * Character class: non-whitespace@b@ */@b@ public static final int NONWHITESPACE = 3;@b@ /**@b@ * Character class: XML whitespace@b@ */@b@ public static final int XMLWHITESPACE = 4;@b@@b@@b@ /** null char */@b@ public static final char NULL_CHAR = '\u0000';@b@ /** linefeed character */@b@ public static final char LINEFEED_CHAR = '\n';@b@ /** carriage return */@b@ public static final char CARRIAGE_RETURN = '\r';@b@ /** normal tab */@b@ public static final char TAB = '\t';@b@ /** normal space */@b@ public static final char SPACE = '\u0020';@b@ /** non-breaking space */@b@ public static final char NBSPACE = '\u00A0';@b@ /** next line control character */@b@ public static final char NEXT_LINE = '\u0085';@b@ /** zero-width space */@b@ public static final char ZERO_WIDTH_SPACE = '\u200B';@b@ /** word joiner */@b@ public static final char WORD_JOINER = '\u2060';@b@ /** zero-width joiner */@b@ public static final char ZERO_WIDTH_JOINER = '\u200D';@b@ /** left-to-right mark */@b@ public static final char LRM = '\u200E';@b@ /** right-to-left mark */@b@ public static final char RLM = '\u202F';@b@ /** left-to-right embedding */@b@ public static final char LRE = '\u202A';@b@ /** right-to-left embedding */@b@ public static final char RLE = '\u202B';@b@ /** pop directional formatting */@b@ public static final char PDF = '\u202C';@b@ /** left-to-right override */@b@ public static final char LRO = '\u202D';@b@ /** right-to-left override */@b@ public static final char RLO = '\u202E';@b@ /** zero-width no-break space (= byte order mark) */@b@ public static final char ZERO_WIDTH_NOBREAK_SPACE = '\uFEFF';@b@ /** soft hyphen */@b@ public static final char SOFT_HYPHEN = '\u00AD';@b@ /** line-separator */@b@ public static final char LINE_SEPARATOR = '\u2028';@b@ /** paragraph-separator */@b@ public static final char PARAGRAPH_SEPARATOR = '\u2029';@b@ /** missing ideograph */@b@ public static final char MISSING_IDEOGRAPH = '\u25A1';@b@ /** Ideogreaphic space */@b@ public static final char IDEOGRAPHIC_SPACE = '\u3000';@b@ /** Object replacement character */@b@ public static final char OBJECT_REPLACEMENT_CHARACTER = '\uFFFC';@b@ /** Unicode value indicating the the character is "not a character". */@b@ public static final char NOT_A_CHARACTER = '\uFFFF';@b@@b@ /**@b@ * Utility class: Constructor prevents instantiating when subclassed.@b@ */@b@ protected CharUtilities() {@b@ throw new UnsupportedOperationException();@b@ }@b@@b@ /**@b@ * Return the appropriate CharClass constant for the type@b@ * of the passed character.@b@ * @param c character to inspect@b@ * @return the determined character class@b@ */@b@ public static int classOf(int c) {@b@ switch (c) {@b@ case CODE_EOT:@b@ return EOT;@b@ case LINEFEED_CHAR:@b@ return LINEFEED;@b@ case SPACE:@b@ case CARRIAGE_RETURN:@b@ case TAB:@b@ return XMLWHITESPACE;@b@ default:@b@ return isAnySpace(c) ? UCWHITESPACE : NONWHITESPACE;@b@ }@b@ }@b@@b@@b@ /**@b@ * Helper method to determine if the character is a@b@ * space with normal behavior. Normal behavior means that@b@ * it's not non-breaking.@b@ * @param c character to inspect@b@ * @return True if the character is a normal space@b@ */@b@ public static boolean isBreakableSpace(int c) {@b@ return (c == SPACE || isFixedWidthSpace(c));@b@ }@b@@b@ /**@b@ * Method to determine if the character is a zero-width space.@b@ * @param c the character to check@b@ * @return true if the character is a zero-width space@b@ */@b@ public static boolean isZeroWidthSpace(int c) {@b@ return c == ZERO_WIDTH_SPACE // 200Bh@b@ || c == WORD_JOINER // 2060h@b@ || c == ZERO_WIDTH_NOBREAK_SPACE; // FEFFh (also used as BOM)@b@ }@b@@b@ /**@b@ * Method to determine if the character is a (breakable) fixed-width space.@b@ * @param c the character to check@b@ * @return true if the character has a fixed-width@b@ */@b@ public static boolean isFixedWidthSpace(int c) {@b@ return (c >= '\u2000' && c <= '\u200B')@b@ || c == '\u3000';@b@// c == '\u2000' // en quad@b@// c == '\u2001' // em quad@b@// c == '\u2002' // en space@b@// c == '\u2003' // em space@b@// c == '\u2004' // three-per-em space@b@// c == '\u2005' // four-per-em space@b@// c == '\u2006' // six-per-em space@b@// c == '\u2007' // figure space@b@// c == '\u2008' // punctuation space@b@// c == '\u2009' // thin space@b@// c == '\u200A' // hair space@b@// c == '\u200B' // zero width space@b@// c == '\u3000' // ideographic space@b@ }@b@@b@ /**@b@ * Method to determine if the character is a nonbreaking@b@ * space.@b@ * @param c character to check@b@ * @return True if the character is a nbsp@b@ */@b@ public static boolean isNonBreakableSpace(int c) {@b@ return@b@ (c == NBSPACE // no-break space@b@ || c == '\u202F' // narrow no-break space@b@ || c == '\u3000' // ideographic space@b@ || c == WORD_JOINER // word joiner@b@ || c == ZERO_WIDTH_NOBREAK_SPACE); // zero width no-break space@b@ }@b@@b@ /**@b@ * Method to determine if the character is an adjustable@b@ * space.@b@ * @param c character to check@b@ * @return True if the character is adjustable@b@ */@b@ public static boolean isAdjustableSpace(int c) {@b@ //TODO: are there other kinds of adjustable spaces?@b@ return@b@ (c == '\u0020' // normal space@b@ || c == NBSPACE); // no-break space@b@ }@b@@b@ /**@b@ * Determines if the character represents any kind of space.@b@ * @param c character to check@b@ * @return True if the character represents any kind of space@b@ */@b@ public static boolean isAnySpace(int c) {@b@ return (isBreakableSpace(c) || isNonBreakableSpace(c));@b@ }@b@@b@ /**@b@ * Indicates whether a character is classified as "Alphabetic" by the Unicode standard.@b@ * @param c the character@b@ * @return true if the character is "Alphabetic"@b@ */@b@ public static boolean isAlphabetic(int c) {@b@ //http://www.unicode.org/Public/UNIDATA/UCD.html#Alphabetic@b@ //Generated from: Other_Alphabetic + Lu + Ll + Lt + Lm + Lo + Nl@b@ int generalCategory = Character.getType((char)c);@b@ switch (generalCategory) {@b@ case Character.UPPERCASE_LETTER: //Lu@b@ case Character.LOWERCASE_LETTER: //Ll@b@ case Character.TITLECASE_LETTER: //Lt@b@ case Character.MODIFIER_LETTER: //Lm@b@ case Character.OTHER_LETTER: //Lo@b@ case Character.LETTER_NUMBER: //Nl@b@ return true;@b@ default:@b@ //TODO if (ch in Other_Alphabetic) return true; (Probably need ICU4J for that)@b@ //Other_Alphabetic contains mostly more exotic characters@b@ return false;@b@ }@b@ }@b@@b@ /**@b@ * Indicates whether the given character is an explicit break-character@b@ * @param c the character to check@b@ * @return true if the character represents an explicit break@b@ */@b@ public static boolean isExplicitBreak(int c) {@b@ return (c == LINEFEED_CHAR@b@ || c == CARRIAGE_RETURN@b@ || c == NEXT_LINE@b@ || c == LINE_SEPARATOR@b@ || c == PARAGRAPH_SEPARATOR);@b@ }@b@@b@ /**@b@ * Convert a single unicode scalar value to an XML numeric character@b@ * reference. If in the BMP, four digits are used, otherwise 6 digits are used.@b@ * @param c a unicode scalar value@b@ * @return a string representing a numeric character reference@b@ */@b@ public static String charToNCRef(int c) {@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = 0, nDigits = (c > 0xFFFF) ? 6 : 4; i < nDigits; i++, c >>= 4) {@b@ int d = c & 0xF;@b@ char hd;@b@ if (d < 10) {@b@ hd = (char) ((int) '0' + d);@b@ } else {@b@ hd = (char) ((int) 'A' + (d - 10));@b@ }@b@ sb.append(hd);@b@ }@b@ return "&#x" + sb.reverse() + ";";@b@ }@b@@b@ /**@b@ * Convert a string to a sequence of ASCII or XML numeric character references.@b@ * @param s a java string (encoded in UTF-16)@b@ * @return a string representing a sequence of numeric character reference or@b@ * ASCII characters@b@ */@b@ public static String toNCRefs(String s) {@b@ StringBuffer sb = new StringBuffer();@b@ if (s != null) {@b@ for (int i = 0; i < s.length(); i++) {@b@ char c = s.charAt(i);@b@ if ((c >= 32) && (c < 127)) {@b@ if (c == '<') {@b@ sb.append("<");@b@ } else if (c == '>') {@b@ sb.append(">");@b@ } else if (c == '&') {@b@ sb.append("&");@b@ } else {@b@ sb.append(c);@b@ }@b@ } else {@b@ sb.append(charToNCRef(c));@b@ }@b@ }@b@ }@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Pad a string S on left out to width W using padding character PAD.@b@ * @param s string to pad@b@ * @param width width of field to add padding@b@ * @param pad character to use for padding@b@ * @return padded string@b@ */@b@ public static String padLeft(String s, int width, char pad) {@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = s.length(); i < width; i++) {@b@ sb.append(pad);@b@ }@b@ sb.append(s);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Format character for debugging output, which it is prefixed with "0x", padded left with '0'@b@ * and either 4 or 6 hex characters in width according to whether it is in the BMP or not.@b@ * @param c character code@b@ * @return formatted character string@b@ */@b@ public static String format(int c) {@b@ if (c < 1114112) {@b@ return "0x" + padLeft(Integer.toString(c, 16), (c < 65536) ? 4 : 6, '0');@b@ } else {@b@ return "!NOT A CHARACTER!";@b@ }@b@ }@b@@b@ /**@b@ * Determine if two character sequences contain the same characters.@b@ * @param cs1 first character sequence@b@ * @param cs2 second character sequence@b@ * @return true if both sequences have same length and same character sequence@b@ */@b@ public static boolean isSameSequence(CharSequence cs1, CharSequence cs2) {@b@ assert cs1 != null;@b@ assert cs2 != null;@b@ if (cs1.length() != cs2.length()) {@b@ return false;@b@ } else {@b@ for (int i = 0, n = cs1.length(); i < n; i++) {@b@ if (cs1.charAt(i) != cs2.charAt(i)) {@b@ return false;@b@ }@b@ }@b@ return true;@b@ }@b@ }@b@@b@}