| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.serialize.codenorm; |
| 9 | |
| 10 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 11 | import net.sf.saxon.z.IntHashMap; |
| 12 | import net.sf.saxon.z.IntToIntMap; |
| 13 | |
| 14 | import java.util.BitSet; |
| 15 | |
| 16 | /** |
| 17 | * Accesses the Normalization Data used for Forms C and D. |
| 18 | * <p>Copyright (c) 1998-1999 Unicode, Inc. All Rights Reserved.<br> |
| 19 | * The Unicode Consortium makes no expressed or implied warranty of any |
| 20 | * kind, and assumes no liability for errors or omissions. |
| 21 | * No liability is assumed for incidental and consequential damages |
| 22 | * in connection with or arising out of the use of the information here.</p> |
| 23 | * |
| 24 | * @author Mark Davis |
| 25 | */ |
| 26 | public class NormalizerData { |
| 27 | static final String copyright = "Copyright (c) 1998-1999 Unicode, Inc."; |
| 28 | |
| 29 | /** |
| 30 | * Constant for use in getPairwiseComposition |
| 31 | */ |
| 32 | public static final int NOT_COMPOSITE = '\uFFFF'; |
| 33 | |
| 34 | /** |
| 35 | * Gets the combining class of a character from the |
| 36 | * Unicode Character Database. |
| 37 | * |
| 38 | * @param ch the source character |
| 39 | * @return value from 0 to 255 |
| 40 | */ |
| 41 | public int getCanonicalClass(int ch) { |
| 42 | return canonicalClass.get(ch); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the composite of the two characters. If the two |
| 47 | * characters don't combine, returns NOT_COMPOSITE. |
| 48 | * Only has to worry about BMP characters, since those are the only ones that can ever compose. |
| 49 | * |
| 50 | * @param first first character (e.g. 'c') |
| 51 | * @param second second character (e.g. '�' cedilla) |
| 52 | * @return composite (e.g. '�') |
| 53 | */ |
| 54 | public char getPairwiseComposition(int first, int second) { |
| 55 | if (first < 0 || first > 0x10FFFF || second < 0 || second > 0x10FFFF) return NOT_COMPOSITE; |
| 56 | return (char) compose.get((first << 16) | second); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets recursive decomposition of a character from the |
| 61 | * Unicode Character Database. |
| 62 | * |
| 63 | * @param canonical If true |
| 64 | * bit is on in this byte, then selects the recursive |
| 65 | * canonical decomposition, otherwise selects |
| 66 | * the recursive compatibility and canonical decomposition. |
| 67 | * @param ch the source character |
| 68 | * @param buffer buffer to be filled with the decomposition |
| 69 | */ |
| 70 | public void getRecursiveDecomposition(boolean canonical, int ch, FastStringBuffer buffer) { |
| 71 | String decomp = (String) decompose.get(ch); |
| 72 | if (decomp != null && !(canonical && isCompatibility.get(ch))) { |
| 73 | for (int i = 0; i < decomp.length(); ++i) { |
| 74 | getRecursiveDecomposition(canonical, decomp.charAt(i), buffer); |
| 75 | } |
| 76 | } else { // if no decomp, append |
| 77 | buffer.appendWideChar(ch); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // ================================================= |
| 82 | // PRIVATES |
| 83 | // ================================================= |
| 84 | |
| 85 | /** |
| 86 | * Only accessed by NormalizerBuilder. |
| 87 | */ |
| 88 | NormalizerData(IntToIntMap canonicalClass, IntHashMap decompose, |
| 89 | IntToIntMap compose, BitSet isCompatibility, BitSet isExcluded) { |
| 90 | this.canonicalClass = canonicalClass; |
| 91 | this.decompose = decompose; |
| 92 | this.compose = compose; |
| 93 | this.isCompatibility = isCompatibility; |
| 94 | this.isExcluded = isExcluded; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Just accessible for testing. |
| 99 | */ |
| 100 | boolean getExcluded(char ch) { |
| 101 | return isExcluded.get(ch); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Just accessible for testing. |
| 106 | */ |
| 107 | /*@NotNull*/ String getRawDecompositionMapping(char ch) { |
| 108 | return (String) decompose.get(ch); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * For now, just use IntHashtable |
| 113 | * Two-stage tables would be used in an optimized implementation. |
| 114 | */ |
| 115 | private IntToIntMap canonicalClass; |
| 116 | |
| 117 | /** |
| 118 | * The main data table maps chars to a 32-bit int. |
| 119 | * It holds either a pair: top = first, bottom = second |
| 120 | * or singleton: top = 0, bottom = single. |
| 121 | * If there is no decomposition, the value is 0. |
| 122 | * Two-stage tables would be used in an optimized implementation. |
| 123 | * An optimization could also map chars to a small index, then use that |
| 124 | * index in a small array of ints. |
| 125 | */ |
| 126 | private IntHashMap decompose; |
| 127 | |
| 128 | /** |
| 129 | * Maps from pairs of characters to single. |
| 130 | * If there is no decomposition, the value is NOT_COMPOSITE. |
| 131 | */ |
| 132 | private IntToIntMap compose; |
| 133 | |
| 134 | /** |
| 135 | * Tells whether decomposition is canonical or not. |
| 136 | */ |
| 137 | private BitSet isCompatibility; |
| 138 | |
| 139 | /** |
| 140 | * Tells whether character is script-excluded or not. |
| 141 | * Used only while building, and for testing. |
| 142 | */ |
| 143 | |
| 144 | private BitSet isExcluded; |
| 145 | } |
| 146 | |
| 147 | // * The class is derived from the sample program NormalizerData.java published by the |
| 148 | // * Unicode consortium. That code has been modified so that instead of building the run-time |
| 149 | // * data structures directly, they are written to a Java "source" module, which is then |
| 150 | // * compiled. Also, the ability to construct a condensed version of the data tables has been |
| 151 | // * removed. |