/src/libreoffice/i18nutil/source/utility/widthfolding.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <i18nutil/widthfolding.hxx> |
21 | | #include <com/sun/star/uno/Sequence.hxx> |
22 | | #include "widthfolding_data.h" |
23 | | |
24 | | using namespace com::sun::star::uno; |
25 | | |
26 | | |
27 | | namespace i18nutil { |
28 | | |
29 | | sal_Unicode widthfolding::decompose_ja_voiced_sound_marksChar2Char (sal_Unicode inChar) |
30 | 0 | { |
31 | 0 | if (0x30a0 <= inChar && inChar <= 0x30ff) { |
32 | 0 | sal_Int16 i = inChar - 0x3040; |
33 | 0 | if (decomposition_table[i].decomposited_character_1) |
34 | 0 | return 0xFFFF; |
35 | 0 | } |
36 | 0 | return inChar; |
37 | 0 | } |
38 | | |
39 | | /** |
40 | | * Decompose Japanese specific voiced and semi-voiced sound marks. |
41 | | */ |
42 | | OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >* pOffset ) |
43 | 0 | { |
44 | | // Create a string buffer which can hold nCount * 2 + 1 characters. |
45 | | // Its size may become double of nCount. |
46 | | // The reference count is 1 now. |
47 | 0 | rtl_uString * newStr = rtl_uString_alloc(nCount * 2); |
48 | |
|
49 | 0 | sal_Int32 *p = nullptr; |
50 | 0 | sal_Int32 position = 0; |
51 | 0 | if (pOffset) { |
52 | | // Allocate double of nCount length to offset argument. |
53 | 0 | pOffset->realloc( nCount * 2 ); |
54 | 0 | p = pOffset->getArray(); |
55 | 0 | position = startPos; |
56 | 0 | } |
57 | | |
58 | | // Prepare pointers of unicode character arrays. |
59 | 0 | const sal_Unicode* src = inStr.getStr() + startPos; |
60 | 0 | sal_Unicode* dst = newStr->buffer; |
61 | | |
62 | | // Decomposition: GA --> KA + voice-mark |
63 | 0 | while (nCount -- > 0) { |
64 | 0 | sal_Unicode c = *src++; |
65 | | // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F) |
66 | | // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF) |
67 | | // Hiragana is not applied to decomposition. |
68 | | // Only Katakana is applied to decomposition |
69 | 0 | if (0x30a0 <= c && c <= 0x30ff) { |
70 | 0 | int i = int(c - 0x3040); |
71 | 0 | sal_Unicode first = decomposition_table[i].decomposited_character_1; |
72 | 0 | if (first != 0x0000) { |
73 | 0 | *dst ++ = first; |
74 | 0 | *dst ++ = decomposition_table[i].decomposited_character_2; // second |
75 | 0 | if (pOffset) { |
76 | 0 | *p ++ = position; |
77 | 0 | *p ++ = position ++; |
78 | 0 | } |
79 | 0 | continue; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | *dst ++ = c; |
83 | 0 | if (pOffset) |
84 | 0 | *p ++ = position ++; |
85 | 0 | } |
86 | 0 | *dst = u'\0'; |
87 | |
|
88 | 0 | newStr->length = sal_Int32(dst - newStr->buffer); |
89 | 0 | if (pOffset) |
90 | 0 | pOffset->realloc(newStr->length); |
91 | 0 | return OUString(newStr, SAL_NO_ACQUIRE); // take ownership |
92 | 0 | } |
93 | | |
94 | | oneToOneMapping& widthfolding::getfull2halfTable() |
95 | 0 | { |
96 | 0 | static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_NORMAL); |
97 | 0 | table.makeIndex(); |
98 | 0 | return table; |
99 | 0 | } |
100 | | |
101 | | /** |
102 | | * Compose Japanese specific voiced and semi-voiced sound marks. |
103 | | */ |
104 | | OUString widthfolding::compose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >* pOffset, sal_Int32 nFlags ) |
105 | 0 | { |
106 | | // Create a string buffer which can hold nCount + 1 characters. |
107 | | // Its size may become equal to nCount or smaller. |
108 | | // The reference count is 1 now. |
109 | 0 | rtl_uString * newStr = rtl_uString_alloc(nCount); |
110 | | |
111 | | // Prepare pointers of unicode character arrays. |
112 | 0 | const sal_Unicode* src = inStr.getStr() + startPos; |
113 | 0 | sal_Unicode* dst = newStr->buffer; |
114 | | |
115 | | // This conversion algorithm requires at least one character. |
116 | 0 | if (nCount > 0) { |
117 | | |
118 | | // .. .. KA VOICE .. .. |
119 | | // ^ ^ |
120 | | // previousChar currentChar |
121 | | // ^ |
122 | | // position |
123 | | // |
124 | | // will be converted to |
125 | | // .. .. GA .. .. |
126 | |
|
127 | 0 | sal_Int32 *p = nullptr; |
128 | 0 | sal_Int32 position = 0; |
129 | 0 | if (pOffset) { |
130 | | // Allocate nCount length to offset argument. |
131 | 0 | pOffset->realloc( nCount ); |
132 | 0 | p = pOffset->getArray(); |
133 | 0 | position = startPos; |
134 | 0 | } |
135 | | |
136 | | // |
137 | 0 | sal_Unicode previousChar = *src ++; |
138 | 0 | sal_Unicode currentChar; |
139 | | |
140 | | // Composition: KA + voice-mark --> GA |
141 | 0 | while (-- nCount > 0) { |
142 | 0 | currentChar = *src ++; |
143 | | // see http://charts.unicode.org/Web/U3040.html Hiragana (U+3040..U+309F) |
144 | | // see http://charts.unicode.org/Web/U30A0.html Katakana (U+30A0..U+30FF) |
145 | | // 0x3099 COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK |
146 | | // 0x309a COMBINING KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK |
147 | | // 0x309b KATAKANA-HIRAGANA VOICED SOUND MARK |
148 | | // 0x309c KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK |
149 | 0 | int j = currentChar - 0x3099; // 0x3099, 0x309a, 0x309b, 0x309c ? |
150 | |
|
151 | 0 | if (2 <= j && j <= 3) // 0x309b or 0x309c |
152 | 0 | j -= 2; |
153 | |
|
154 | 0 | if (0 <= j && j <= 1) { |
155 | | // 0 addresses a code point regarding 0x3099 or 0x309b (voiced sound mark), |
156 | | // 1 is 0x309a or 0x309c (semi-voiced sound mark) |
157 | 0 | int i = int(previousChar - 0x3040); // i acts as an index of array |
158 | 0 | bool bCompose = false; |
159 | |
|
160 | 0 | if (0 <= i && i <= (0x30ff - 0x3040) && composition_table[i][j]) |
161 | 0 | bCompose = true; |
162 | | |
163 | | // not to use combined KATAKANA LETTER VU |
164 | 0 | if ( previousChar == 0x30a6 && (nFlags & WIDTHFOLDING_DONT_USE_COMBINED_VU) ) |
165 | 0 | bCompose = false; |
166 | |
|
167 | 0 | if( bCompose ){ |
168 | 0 | if (pOffset) { |
169 | 0 | position ++; |
170 | 0 | *p ++ = position ++; |
171 | 0 | } |
172 | 0 | *dst ++ = composition_table[i][j]; |
173 | 0 | previousChar = *src ++; |
174 | 0 | nCount --; |
175 | 0 | continue; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | if (pOffset) |
179 | 0 | *p ++ = position ++; |
180 | 0 | *dst ++ = previousChar; |
181 | 0 | previousChar = currentChar; |
182 | 0 | } |
183 | |
|
184 | 0 | if (nCount == 0) { |
185 | 0 | if (pOffset) |
186 | 0 | *p = position; |
187 | 0 | *dst ++ = previousChar; |
188 | 0 | } |
189 | |
|
190 | 0 | *dst = u'\0'; |
191 | |
|
192 | 0 | newStr->length = sal_Int32(dst - newStr->buffer); |
193 | 0 | } |
194 | 0 | if (pOffset) |
195 | 0 | pOffset->realloc(newStr->length); |
196 | 0 | return OUString(newStr, SAL_NO_ACQUIRE); // take ownership |
197 | 0 | } |
198 | | |
199 | | oneToOneMapping& widthfolding::gethalf2fullTable() |
200 | 0 | { |
201 | 0 | static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_NORMAL); |
202 | 0 | table.makeIndex(); |
203 | 0 | return table; |
204 | 0 | } |
205 | | |
206 | | sal_Unicode widthfolding::getCompositionChar(sal_Unicode c1, sal_Unicode c2) |
207 | 0 | { |
208 | 0 | return composition_table[c1 - 0x3040][c2 - 0x3099]; |
209 | 0 | } |
210 | | |
211 | | |
212 | | oneToOneMapping& widthfolding::getfull2halfTableForASC() |
213 | 0 | { |
214 | 0 | static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_ASC_FUNCTION); |
215 | 0 | table.makeIndex(); |
216 | |
|
217 | 0 | return table; |
218 | 0 | } |
219 | | |
220 | | oneToOneMapping& widthfolding::gethalf2fullTableForJIS() |
221 | 0 | { |
222 | 0 | static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_JIS_FUNCTION); |
223 | 0 | table.makeIndex(); |
224 | |
|
225 | 0 | return table; |
226 | 0 | } |
227 | | |
228 | | oneToOneMapping& widthfolding::getfullKana2halfKanaTable() |
229 | 0 | { |
230 | 0 | static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_KATAKANA_ONLY); |
231 | 0 | table.makeIndex(); |
232 | 0 | return table; |
233 | 0 | } |
234 | | |
235 | | oneToOneMapping& widthfolding::gethalfKana2fullKanaTable() |
236 | 0 | { |
237 | 0 | static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_KATAKANA_ONLY); |
238 | 0 | table.makeIndex(); |
239 | 0 | return table; |
240 | 0 | } |
241 | | |
242 | | } |
243 | | |
244 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |