Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / i18nutil / source / utility / widthfolding.cxx
blob628888199fb1bb2f1e388938bf0710bd0e4e2ae4
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;
28 using ::rtl::OUString;
30 namespace com { namespace sun { namespace star { namespace i18n {
32 sal_Unicode widthfolding::decompose_ja_voiced_sound_marksChar2Char (sal_Unicode inChar)
34 if (0x30a0 <= inChar && inChar <= 0x30ff) {
35 sal_Int16 i = inChar - 0x3040;
36 if (decomposition_table[i].decomposited_character_1)
37 return 0xFFFF;
39 return inChar;
42 /**
43 * Decompose Japanese specific voiced and semi-voiced sound marks.
45 OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, sal_Bool useOffset )
47 // Create a string buffer which can hold nCount * 2 + 1 characters.
48 // Its size may become double of nCount.
49 // The reference count is 1 now.
50 rtl_uString * newStr = comphelper::string::rtl_uString_alloc(nCount * 2);
52 sal_Int32 *p = NULL;
53 sal_Int32 position = 0;
54 if (useOffset) {
55 // Allocate double of nCount length to offset argument.
56 offset.realloc( nCount * 2 );
57 p = offset.getArray();
58 position = startPos;
61 // Prepare pointers of unicode character arrays.
62 const sal_Unicode* src = inStr.getStr() + startPos;
63 sal_Unicode* dst = newStr->buffer;
65 // Decomposition: GA --> KA + voice-mark
66 while (nCount -- > 0) {
67 sal_Unicode c = *src++;
68 // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F)
69 // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF)
70 // Hiragana is not applied to decomposition.
71 // Only Katakana is applied to decomposition
72 if (0x30a0 <= c && c <= 0x30ff) {
73 int i = int(c - 0x3040);
74 sal_Unicode first = decomposition_table[i].decomposited_character_1;
75 if (first != 0x0000) {
76 *dst ++ = first;
77 *dst ++ = decomposition_table[i].decomposited_character_2; // second
78 if (useOffset) {
79 *p ++ = position;
80 *p ++ = position ++;
82 continue;
85 *dst ++ = c;
86 if (useOffset)
87 *p ++ = position ++;
89 *dst = (sal_Unicode) 0;
91 newStr->length = sal_Int32(dst - newStr->buffer);
92 if (useOffset)
93 offset.realloc(newStr->length);
94 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
97 oneToOneMapping& widthfolding::getfull2halfTable(void)
99 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_NORMAL);
100 table.makeIndex();
101 return table;
105 * Compose Japanese specific voiced and semi-voiced sound marks.
107 OUString widthfolding::compose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, sal_Bool useOffset, sal_Int32 nFlags )
109 // Create a string buffer which can hold nCount + 1 characters.
110 // Its size may become equal to nCount or smaller.
111 // The reference count is 1 now.
112 rtl_uString * newStr = comphelper::string::rtl_uString_alloc(nCount);
114 // Prepare pointers of unicode character arrays.
115 const sal_Unicode* src = inStr.getStr() + startPos;
116 sal_Unicode* dst = newStr->buffer;
118 // This conversion algorithm requires at least one character.
119 if (nCount > 0) {
121 // .. .. KA VOICE .. ..
122 // ^ ^
123 // previousChar currentChar
124 // ^
125 // position
127 // will be converted to
128 // .. .. GA .. ..
130 sal_Int32 *p = NULL;
131 sal_Int32 position = 0;
132 if (useOffset) {
133 // Allocate nCount length to offset argument.
134 offset.realloc( nCount );
135 p = offset.getArray();
136 position = startPos;
140 sal_Unicode previousChar = *src ++;
141 sal_Unicode currentChar;
143 // Composition: KA + voice-mark --> GA
144 while (-- nCount > 0) {
145 currentChar = *src ++;
146 // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F)
147 // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF)
148 // 0x3099 COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK
149 // 0x309a COMBINING KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
150 // 0x309b KATAKANA-HIRAGANA VOICED SOUND MARK
151 // 0x309c KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
152 int j = currentChar - 0x3099; // 0x3099, 0x309a, 0x309b, 0x309c ?
154 if (2 <= j && j <= 3) // 0x309b or 0x309c
155 j -= 2;
157 if (0 <= j && j <= 1) {
158 // 0 addresses a code point regarding 0x3099 or 0x309b (voiced sound mark),
159 // 1 is 0x309a or 0x309c (semi-voiced sound mark)
160 int i = int(previousChar - 0x3040); // i acts as an index of array
161 sal_Bool bCompose = sal_False;
163 if (0 <= i && i <= (0x30ff - 0x3040) && composition_table[i][j])
164 bCompose = sal_True;
166 // not to use combined KATAKANA LETTER VU
167 if ( previousChar == 0x30a6 && (nFlags & WIDTHFOLDNIG_DONT_USE_COMBINED_VU) )
168 bCompose = sal_False;
170 if( bCompose ){
171 if (useOffset) {
172 position ++;
173 *p ++ = position ++;
175 *dst ++ = composition_table[i][j];
176 previousChar = *src ++;
177 nCount --;
178 continue;
181 if (useOffset)
182 *p ++ = position ++;
183 *dst ++ = previousChar;
184 previousChar = currentChar;
187 if (nCount == 0) {
188 if (useOffset)
189 *p = position;
190 *dst ++ = previousChar;
193 *dst = (sal_Unicode) 0;
195 newStr->length = sal_Int32(dst - newStr->buffer);
197 if (useOffset)
198 offset.realloc(newStr->length);
199 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
202 oneToOneMapping& widthfolding::gethalf2fullTable(void)
204 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_NORMAL);
205 table.makeIndex();
206 return table;
209 sal_Unicode widthfolding::getCompositionChar(sal_Unicode c1, sal_Unicode c2)
211 return composition_table[c1 - 0x3040][c2 - 0x3099];
215 oneToOneMapping& widthfolding::getfull2halfTableForASC()
217 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_ASC_FUNCTION);
218 table.makeIndex();
220 // bluedwarf: dirty hack!
221 // There is an exception. Additional conversion is required following:
222 // 0xFFE5 (FULLWIDTH YEN SIGN) --> 0x005C (REVERSE SOLIDUS)
224 // See the following page for detail:
225 // http://wiki.services.openoffice.org/wiki/Calc/Features/JIS_and_ASC_functions
226 int i, j, high, low;
227 int n = sizeof(full2halfASCException) / sizeof(UnicodePairWithFlag);
228 for( i = 0; i < n; i++ )
230 high = (full2halfASCException[i].first >> 8) & 0xFF;
231 low = (full2halfASCException[i].first) & 0xFF;
233 if( !table.mpIndex[high] )
235 table.mpIndex[high] = new UnicodePairWithFlag*[256];
237 for( j = 0; j < 256; j++ )
238 table.mpIndex[high][j] = NULL;
240 table.mpIndex[high][low] = &full2halfASCException[i];
243 return table;
246 oneToOneMapping& widthfolding::gethalf2fullTableForJIS()
248 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_JIS_FUNCTION);
249 table.makeIndex();
251 // bluedwarf: dirty hack!
252 // There are some exceptions. Additional conversion are required following:
253 // 0x0022 (QUOTATION MARK) --> 0x201D (RIGHT DOUBLE QUOTATION MARK)
254 // 0x0027 (APOSTROPHE) --> 0x2019 (RIGHT SINGLE QUOTATION MARK)
255 // 0x005C (REVERSE SOLIDUS) --> 0xFFE5 (FULLWIDTH YEN SIGN)
256 // 0x0060 (GRAVE ACCENT) --> 0x2018 (LEFT SINGLE QUOTATION MARK)
258 // See the following page for detail:
259 // http://wiki.services.openoffice.org/wiki/Calc/Features/JIS_and_ASC_functions
260 int i, j, high, low;
261 int n = sizeof(half2fullJISException) / sizeof(UnicodePairWithFlag);
262 for( i = 0; i < n; i++ )
264 high = (half2fullJISException[i].first >> 8) & 0xFF;
265 low = (half2fullJISException[i].first) & 0xFF;
267 if( !table.mpIndex[high] )
269 table.mpIndex[high] = new UnicodePairWithFlag*[256];
271 for( j = 0; j < 256; j++ )
272 table.mpIndex[high][j] = NULL;
274 table.mpIndex[high][low] = &half2fullJISException[i];
277 return table;
280 oneToOneMapping& widthfolding::getfullKana2halfKanaTable()
282 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_KATAKANA_ONLY);
283 table.makeIndex();
284 return table;
287 oneToOneMapping& widthfolding::gethalfKana2fullKanaTable()
289 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_KATAKANA_ONLY);
290 table.makeIndex();
291 return table;
294 } } } }
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */