Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / i18nutil / source / utility / widthfolding.cxx
blob8b3e1cb816ef984a25df10e1ae68b477aaede7bc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // prevent internal compiler error with MSVC6SP3
21 #include <utility>
22 #include <i18nutil/widthfolding.hxx>
23 #include <comphelper/string.hxx>
24 #include "widthfolding_data.h"
26 using namespace com::sun::star::uno;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 sal_Unicode widthfolding::decompose_ja_voiced_sound_marksChar2Char (sal_Unicode inChar)
33 if (0x30a0 <= inChar && inChar <= 0x30ff) {
34 sal_Int16 i = inChar - 0x3040;
35 if (decomposition_table[i].decomposited_character_1)
36 return 0xFFFF;
38 return inChar;
41 /**
42 * Decompose Japanese specific voiced and semi-voiced sound marks.
44 OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, bool useOffset )
46 // Create a string buffer which can hold nCount * 2 + 1 characters.
47 // Its size may become double of nCount.
48 // The reference count is 1 now.
49 rtl_uString * newStr = rtl_uString_alloc(nCount * 2);
51 sal_Int32 *p = nullptr;
52 sal_Int32 position = 0;
53 if (useOffset) {
54 // Allocate double of nCount length to offset argument.
55 offset.realloc( nCount * 2 );
56 p = offset.getArray();
57 position = startPos;
60 // Prepare pointers of unicode character arrays.
61 const sal_Unicode* src = inStr.getStr() + startPos;
62 sal_Unicode* dst = newStr->buffer;
64 // Decomposition: GA --> KA + voice-mark
65 while (nCount -- > 0) {
66 sal_Unicode c = *src++;
67 // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F)
68 // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF)
69 // Hiragana is not applied to decomposition.
70 // Only Katakana is applied to decomposition
71 if (0x30a0 <= c && c <= 0x30ff) {
72 int i = int(c - 0x3040);
73 sal_Unicode first = decomposition_table[i].decomposited_character_1;
74 if (first != 0x0000) {
75 *dst ++ = first;
76 *dst ++ = decomposition_table[i].decomposited_character_2; // second
77 if (useOffset) {
78 *p ++ = position;
79 *p ++ = position ++;
81 continue;
84 *dst ++ = c;
85 if (useOffset)
86 *p ++ = position ++;
88 *dst = (sal_Unicode) 0;
90 newStr->length = sal_Int32(dst - newStr->buffer);
91 if (useOffset)
92 offset.realloc(newStr->length);
93 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
96 oneToOneMapping& widthfolding::getfull2halfTable()
98 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_NORMAL);
99 table.makeIndex();
100 return table;
104 * Compose Japanese specific voiced and semi-voiced sound marks.
106 OUString widthfolding::compose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, bool useOffset, sal_Int32 nFlags )
108 // Create a string buffer which can hold nCount + 1 characters.
109 // Its size may become equal to nCount or smaller.
110 // The reference count is 1 now.
111 rtl_uString * newStr = rtl_uString_alloc(nCount);
113 // Prepare pointers of unicode character arrays.
114 const sal_Unicode* src = inStr.getStr() + startPos;
115 sal_Unicode* dst = newStr->buffer;
117 // This conversion algorithm requires at least one character.
118 if (nCount > 0) {
120 // .. .. KA VOICE .. ..
121 // ^ ^
122 // previousChar currentChar
123 // ^
124 // position
126 // will be converted to
127 // .. .. GA .. ..
129 sal_Int32 *p = nullptr;
130 sal_Int32 position = 0;
131 if (useOffset) {
132 // Allocate nCount length to offset argument.
133 offset.realloc( nCount );
134 p = offset.getArray();
135 position = startPos;
139 sal_Unicode previousChar = *src ++;
140 sal_Unicode currentChar;
142 // Composition: KA + voice-mark --> GA
143 while (-- nCount > 0) {
144 currentChar = *src ++;
145 // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F)
146 // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF)
147 // 0x3099 COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK
148 // 0x309a COMBINING KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
149 // 0x309b KATAKANA-HIRAGANA VOICED SOUND MARK
150 // 0x309c KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
151 int j = currentChar - 0x3099; // 0x3099, 0x309a, 0x309b, 0x309c ?
153 if (2 <= j && j <= 3) // 0x309b or 0x309c
154 j -= 2;
156 if (0 <= j && j <= 1) {
157 // 0 addresses a code point regarding 0x3099 or 0x309b (voiced sound mark),
158 // 1 is 0x309a or 0x309c (semi-voiced sound mark)
159 int i = int(previousChar - 0x3040); // i acts as an index of array
160 bool bCompose = false;
162 if (0 <= i && i <= (0x30ff - 0x3040) && composition_table[i][j])
163 bCompose = true;
165 // not to use combined KATAKANA LETTER VU
166 if ( previousChar == 0x30a6 && (nFlags & WIDTHFOLDNIG_DONT_USE_COMBINED_VU) )
167 bCompose = false;
169 if( bCompose ){
170 if (useOffset) {
171 position ++;
172 *p ++ = position ++;
174 *dst ++ = composition_table[i][j];
175 previousChar = *src ++;
176 nCount --;
177 continue;
180 if (useOffset)
181 *p ++ = position ++;
182 *dst ++ = previousChar;
183 previousChar = currentChar;
186 if (nCount == 0) {
187 if (useOffset)
188 *p = position;
189 *dst ++ = previousChar;
192 *dst = (sal_Unicode) 0;
194 newStr->length = sal_Int32(dst - newStr->buffer);
196 if (useOffset)
197 offset.realloc(newStr->length);
198 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
201 oneToOneMapping& widthfolding::gethalf2fullTable()
203 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_NORMAL);
204 table.makeIndex();
205 return table;
208 sal_Unicode widthfolding::getCompositionChar(sal_Unicode c1, sal_Unicode c2)
210 return composition_table[c1 - 0x3040][c2 - 0x3099];
214 oneToOneMapping& widthfolding::getfull2halfTableForASC()
216 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_ASC_FUNCTION);
217 table.makeIndex();
219 // bluedwarf: dirty hack!
220 // There is an exception. Additional conversion is required following:
221 // 0xFFE5 (FULLWIDTH YEN SIGN) --> 0x005C (REVERSE SOLIDUS)
223 // See the following page for detail:
224 // http://wiki.openoffice.org/wiki/Calc/Features/JIS_and_ASC_functions
225 int i, j;
226 int n = sizeof(full2halfASCException) / sizeof(UnicodePairWithFlag);
227 for( i = 0; i < n; i++ )
229 const int high = (full2halfASCException[i].first >> 8) & 0xFF;
230 const int low = (full2halfASCException[i].first) & 0xFF;
232 if( !table.mpIndex[high] )
234 table.mpIndex[high] = new UnicodePairWithFlag*[256];
236 for( j = 0; j < 256; j++ )
237 table.mpIndex[high][j] = nullptr;
239 table.mpIndex[high][low] = &full2halfASCException[i];
242 return table;
245 oneToOneMapping& widthfolding::gethalf2fullTableForJIS()
247 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_JIS_FUNCTION);
248 table.makeIndex();
250 // bluedwarf: dirty hack!
251 // There are some exceptions. Additional conversion are required following:
252 // 0x0022 (QUOTATION MARK) --> 0x201D (RIGHT DOUBLE QUOTATION MARK)
253 // 0x0027 (APOSTROPHE) --> 0x2019 (RIGHT SINGLE QUOTATION MARK)
254 // 0x005C (REVERSE SOLIDUS) --> 0xFFE5 (FULLWIDTH YEN SIGN)
255 // 0x0060 (GRAVE ACCENT) --> 0x2018 (LEFT SINGLE QUOTATION MARK)
257 // See the following page for detail:
258 // http://wiki.openoffice.org/wiki/Calc/Features/JIS_and_ASC_functions
259 int i, j;
260 int n = sizeof(half2fullJISException) / sizeof(UnicodePairWithFlag);
261 for( i = 0; i < n; i++ )
263 const int high = (half2fullJISException[i].first >> 8) & 0xFF;
264 const int low = (half2fullJISException[i].first) & 0xFF;
266 if( !table.mpIndex[high] )
268 table.mpIndex[high] = new UnicodePairWithFlag*[256];
270 for( j = 0; j < 256; j++ )
271 table.mpIndex[high][j] = nullptr;
273 table.mpIndex[high][low] = &half2fullJISException[i];
276 return table;
279 oneToOneMapping& widthfolding::getfullKana2halfKanaTable()
281 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_KATAKANA_ONLY);
282 table.makeIndex();
283 return table;
286 oneToOneMapping& widthfolding::gethalfKana2fullKanaTable()
288 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_KATAKANA_ONLY);
289 table.makeIndex();
290 return table;
293 } } } }
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */