1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include "i18nutil/casefolding.hxx" 25 #include "casefolding_data.h" 26 #include "i18nutil/widthfolding.hxx" 27 28 using namespace com::sun::star::lang; 29 using namespace com::sun::star::uno; 30 31 namespace com { namespace sun { namespace star { namespace i18n { 32 33 static Mapping mapping_03a3[] = {{0, 1, {0x03c2, 0, 0}},{0, 1, {0x03c3, 0, 0}}}; 34 static Mapping mapping_0307[] = {{0, 0, {0, 0, 0}},{0, 1, {0x0307, 0, 0}}}; 35 static Mapping mapping_004a[] = {{0, 2, {0x006a, 0x0307, 0}},{0, 1, {0x006a, 0, 0}}}; 36 static Mapping mapping_012e[] = {{0, 2, {0x012f, 0x0307, 0}},{0, 1, {0x012f, 0, 0}}}; 37 static Mapping mapping_00cc[] = {{0, 3, {0x0069, 0x0307, 0x0300}},{0, 1, {0x00ec, 0, 0}}}; 38 static Mapping mapping_00cd[] = {{0, 3, {0x0069, 0x0307, 0x0301}},{0, 1, {0x00ed, 0, 0}}}; 39 static Mapping mapping_0128[] = {{0, 3, {0x0069, 0x0307, 0x0303}},{0, 1, {0x0129, 0, 0}}}; 40 static Mapping mapping_0049[] = {{0, 2, {0x0069, 0x0307, 0}},{0, 1, {0x0131, 0, 0}},{0, 1, {0x0069, 0, 0}}}; 41 static Mapping mapping_0069[] = {{0, 1, {0x0130, 0, 0}},{0, 1, {0x0049, 0, 0}}}; 42 static Mapping mapping_0130[] = {{0, 1, {0x0069, 0, 0}},{0, 1, {0x0130, 0, 0}}}; 43 44 #define langIs(lang) (aLocale.Language.compareToAscii(lang) == 0) 45 46 // only check simple case, there is more complicated case need to be checked. 47 #define type_i(ch) ((ch) == 0x0069 || (ch) == 0x006a) 48 49 #define cased_letter(ch) (CaseMappingIndex[(ch)>>8] >= 0 && (CaseMappingValue[(CaseMappingIndex[(ch)>>8] << 8) + ((ch)&0xff)].type & CasedLetter)) 50 51 // for Lithuanian, condition to make explicit dot above when lowercasing capital I's and J's 52 // whenever there are more accents above. 53 #define accent_above(ch) (((ch) >= 0x0300 && (ch) <= 0x0314) || ((ch) >= 0x033D && (ch) <= 0x0344) || (ch) == 0x0346 || ((ch) >= 0x034A && (ch) <= 0x034C)) 54 55 Mapping& casefolding::getConditionalValue(const sal_Unicode* str, sal_Int32 pos, sal_Int32 len, Locale& aLocale, sal_uInt8 nMappingType) throw (RuntimeException) 56 { 57 switch(str[pos]) { 58 case 0x03a3: 59 // final_sigma (not followed by cased and preceded by cased character) 60 // DOES NOT check ignorable sequence yet (more complicated implementation). 61 return !(pos < len && cased_letter(str[pos+1])) && (pos > 0 && cased_letter(str[pos-1])) ? 62 mapping_03a3[0] : mapping_03a3[1]; 63 case 0x0307: 64 return (((nMappingType == MappingTypeLowerToUpper && langIs("lt")) || 65 (nMappingType == MappingTypeUpperToLower && (langIs("tr") || langIs("az")))) && 66 (pos > 0 && type_i(str[pos-1]))) ? // after_i 67 mapping_0307[0] : mapping_0307[1]; 68 case 0x0130: 69 return (langIs("tr") || langIs("az")) ? mapping_0130[0] : mapping_0130[1]; 70 case 0x0069: 71 return (langIs("tr") || langIs("az")) ? mapping_0069[0] : mapping_0069[1]; 72 case 0x0049: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_0049[0] : 73 (langIs("tr") || langIs("az")) ? mapping_0049[1] : mapping_0049[2]; 74 case 0x004a: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_004a[0] : mapping_004a[1]; 75 case 0x012e: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_012e[0] : mapping_012e[1]; 76 case 0x00cc: return langIs("lt") ? mapping_00cc[0] : mapping_00cc[1]; 77 case 0x00cd: return langIs("lt") ? mapping_00cd[0] : mapping_00cd[1]; 78 case 0x0128: return langIs("lt") ? mapping_0128[0] : mapping_0128[1]; 79 } 80 // Should not come here 81 throw RuntimeException(); 82 } 83 84 Mapping& casefolding::getValue(const sal_Unicode* str, sal_Int32 pos, sal_Int32 len, Locale& aLocale, sal_uInt8 nMappingType) throw (RuntimeException) 85 { 86 static Mapping dummy = { 0, 1, { 0, 0, 0 } }; 87 sal_Int16 address = CaseMappingIndex[str[pos] >> 8] << 8; 88 89 dummy.map[0] = str[pos]; 90 91 if (address >= 0 && (CaseMappingValue[address += (str[pos] & 0xFF)].type & nMappingType)) { 92 sal_uInt8 type = CaseMappingValue[address].type; 93 if (type & ValueTypeNotValue) { 94 if (CaseMappingValue[address].value == 0) 95 return getConditionalValue(str, pos, len, aLocale, nMappingType); 96 else { 97 for (int map = CaseMappingValue[address].value; 98 map < CaseMappingValue[address].value + MaxCaseMappingExtras; map++) { 99 if (CaseMappingExtra[map].type & nMappingType) { 100 if (CaseMappingExtra[map].type & ValueTypeNotValue) 101 return getConditionalValue(str, pos, len, aLocale, nMappingType); 102 else 103 return CaseMappingExtra[map]; 104 } 105 } 106 // Should not come here 107 throw RuntimeException(); 108 } 109 } else 110 dummy.map[0] = CaseMappingValue[address].value; 111 } 112 return dummy; 113 } 114 115 inline sal_Bool SAL_CALL 116 is_ja_voice_sound_mark(sal_Unicode& current, sal_Unicode next) 117 { 118 sal_Unicode c = 0; 119 120 if ((next == 0x3099 || next == 0x309a) && ( (c = widthfolding::getCompositionChar(current, next)) != 0 )) 121 current = c; 122 return c != 0; 123 } 124 125 sal_Unicode casefolding::getNextChar(const sal_Unicode *str, sal_Int32& idx, sal_Int32 len, MappingElement& e, Locale& aLocale, sal_uInt8 nMappingType, TransliterationModules moduleLoaded) throw (RuntimeException) 126 { 127 if( idx >= len ) 128 { 129 e = MappingElement(); 130 return 0; 131 } 132 133 sal_Unicode c; 134 135 if (moduleLoaded & TransliterationModules_IGNORE_CASE) { 136 if( e.current >= e.element.nmap ) { 137 e.element = getValue(str, idx++, len, aLocale, nMappingType); 138 e.current = 0; 139 } 140 c = e.element.map[e.current++]; 141 } else { 142 c = *(str + idx++); 143 } 144 145 if (moduleLoaded & TransliterationModules_IGNORE_KANA) { 146 if ((0x3040 <= c && c <= 0x3094) || (0x309d <= c && c <= 0x309f)) 147 c += 0x60; 148 } 149 150 // composition: KA + voice-mark --> GA. see halfwidthToFullwidth.cxx for detail 151 if (moduleLoaded & TransliterationModules_IGNORE_WIDTH) { 152 static oneToOneMapping& half2fullTable = widthfolding::gethalf2fullTable(); 153 c = half2fullTable[c]; 154 if (0x3040 <= c && c <= 0x30ff && idx < len && 155 is_ja_voice_sound_mark(c, half2fullTable[*(str + idx)])) 156 idx++; 157 } 158 159 return c; 160 } 161 162 } } } } 163 164