Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18nutil / source / utility / widthfolding.cxx
blob6f572080689cd0b08a85d9c81dae7fa75ac0b259
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 #include <i18nutil/widthfolding.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
22 #include "widthfolding_data.h"
24 using namespace com::sun::star::uno;
27 namespace i18nutil {
29 sal_Unicode widthfolding::decompose_ja_voiced_sound_marksChar2Char (sal_Unicode inChar)
31 if (0x30a0 <= inChar && inChar <= 0x30ff) {
32 sal_Int16 i = inChar - 0x3040;
33 if (decomposition_table[i].decomposited_character_1)
34 return 0xFFFF;
36 return inChar;
39 /**
40 * Decompose Japanese specific voiced and semi-voiced sound marks.
42 OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >& offset, bool useOffset )
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 rtl_uString * newStr = rtl_uString_alloc(nCount * 2);
49 sal_Int32 *p = nullptr;
50 sal_Int32 position = 0;
51 if (useOffset) {
52 // Allocate double of nCount length to offset argument.
53 offset.realloc( nCount * 2 );
54 p = offset.getArray();
55 position = startPos;
58 // Prepare pointers of unicode character arrays.
59 const sal_Unicode* src = inStr.getStr() + startPos;
60 sal_Unicode* dst = newStr->buffer;
62 // Decomposition: GA --> KA + voice-mark
63 while (nCount -- > 0) {
64 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 if (0x30a0 <= c && c <= 0x30ff) {
70 int i = int(c - 0x3040);
71 sal_Unicode first = decomposition_table[i].decomposited_character_1;
72 if (first != 0x0000) {
73 *dst ++ = first;
74 *dst ++ = decomposition_table[i].decomposited_character_2; // second
75 if (useOffset) {
76 *p ++ = position;
77 *p ++ = position ++;
79 continue;
82 *dst ++ = c;
83 if (useOffset)
84 *p ++ = position ++;
86 *dst = u'\0';
88 newStr->length = sal_Int32(dst - newStr->buffer);
89 if (useOffset)
90 offset.realloc(newStr->length);
91 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
94 oneToOneMapping& widthfolding::getfull2halfTable()
96 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_NORMAL);
97 table.makeIndex();
98 return table;
102 * Compose Japanese specific voiced and semi-voiced sound marks.
104 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 )
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 rtl_uString * newStr = rtl_uString_alloc(nCount);
111 // Prepare pointers of unicode character arrays.
112 const sal_Unicode* src = inStr.getStr() + startPos;
113 sal_Unicode* dst = newStr->buffer;
115 // This conversion algorithm requires at least one character.
116 if (nCount > 0) {
118 // .. .. KA VOICE .. ..
119 // ^ ^
120 // previousChar currentChar
121 // ^
122 // position
124 // will be converted to
125 // .. .. GA .. ..
127 sal_Int32 *p = nullptr;
128 sal_Int32 position = 0;
129 if (useOffset) {
130 // Allocate nCount length to offset argument.
131 offset.realloc( nCount );
132 p = offset.getArray();
133 position = startPos;
137 sal_Unicode previousChar = *src ++;
138 sal_Unicode currentChar;
140 // Composition: KA + voice-mark --> GA
141 while (-- nCount > 0) {
142 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 int j = currentChar - 0x3099; // 0x3099, 0x309a, 0x309b, 0x309c ?
151 if (2 <= j && j <= 3) // 0x309b or 0x309c
152 j -= 2;
154 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 int i = int(previousChar - 0x3040); // i acts as an index of array
158 bool bCompose = false;
160 if (0 <= i && i <= (0x30ff - 0x3040) && composition_table[i][j])
161 bCompose = true;
163 // not to use combined KATAKANA LETTER VU
164 if ( previousChar == 0x30a6 && (nFlags & WIDTHFOLDING_DONT_USE_COMBINED_VU) )
165 bCompose = false;
167 if( bCompose ){
168 if (useOffset) {
169 position ++;
170 *p ++ = position ++;
172 *dst ++ = composition_table[i][j];
173 previousChar = *src ++;
174 nCount --;
175 continue;
178 if (useOffset)
179 *p ++ = position ++;
180 *dst ++ = previousChar;
181 previousChar = currentChar;
184 if (nCount == 0) {
185 if (useOffset)
186 *p = position;
187 *dst ++ = previousChar;
190 *dst = u'\0';
192 newStr->length = sal_Int32(dst - newStr->buffer);
194 if (useOffset)
195 offset.realloc(newStr->length);
196 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
199 oneToOneMapping& widthfolding::gethalf2fullTable()
201 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_NORMAL);
202 table.makeIndex();
203 return table;
206 sal_Unicode widthfolding::getCompositionChar(sal_Unicode c1, sal_Unicode c2)
208 return composition_table[c1 - 0x3040][c2 - 0x3099];
212 oneToOneMapping& widthfolding::getfull2halfTableForASC()
214 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_ASC_FUNCTION);
215 table.makeIndex();
217 return table;
220 oneToOneMapping& widthfolding::gethalf2fullTableForJIS()
222 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_JIS_FUNCTION);
223 table.makeIndex();
225 return table;
228 oneToOneMapping& widthfolding::getfullKana2halfKanaTable()
230 static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_KATAKANA_ONLY);
231 table.makeIndex();
232 return table;
235 oneToOneMapping& widthfolding::gethalfKana2fullKanaTable()
237 static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_KATAKANA_ONLY);
238 table.makeIndex();
239 return table;
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */