LibreOffice Module i18nutil (master)  1
widthfolding.cxx
Go to the documentation of this file.
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 
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 {
31  if (0x30a0 <= inChar && inChar <= 0x30ff) {
32  sal_Int16 i = inChar - 0x3040;
33  if (decomposition_table[i].decomposited_character_1)
34  return 0xFFFF;
35  }
36  return inChar;
37 }
38 
42 OUString widthfolding::decompose_ja_voiced_sound_marks (const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, Sequence< sal_Int32 >* pOffset )
43 {
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);
48 
49  sal_Int32 *p = nullptr;
50  sal_Int32 position = 0;
51  if (pOffset) {
52  // Allocate double of nCount length to offset argument.
53  pOffset->realloc( nCount * 2 );
54  p = pOffset->getArray();
55  position = startPos;
56  }
57 
58  // Prepare pointers of unicode character arrays.
59  const sal_Unicode* src = inStr.getStr() + startPos;
60  sal_Unicode* dst = newStr->buffer;
61 
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);
72  if (first != 0x0000) {
73  *dst ++ = first;
74  *dst ++ = decomposition_table[i].decomposited_character_2; // second
75  if (pOffset) {
76  *p ++ = position;
77  *p ++ = position ++;
78  }
79  continue;
80  }
81  }
82  *dst ++ = c;
83  if (pOffset)
84  *p ++ = position ++;
85  }
86  *dst = u'\0';
87 
88  newStr->length = sal_Int32(dst - newStr->buffer);
89  if (pOffset)
90  pOffset->realloc(newStr->length);
91  return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
92 }
93 
94 oneToOneMapping& widthfolding::getfull2halfTable()
95 {
96  static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_NORMAL);
97  table.makeIndex();
98  return table;
99 }
100 
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 {
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);
110 
111  // Prepare pointers of unicode character arrays.
112  const sal_Unicode* src = inStr.getStr() + startPos;
113  sal_Unicode* dst = newStr->buffer;
114 
115  // This conversion algorithm requires at least one character.
116  if (nCount > 0) {
117 
118  // .. .. KA VOICE .. ..
119  // ^ ^
120  // previousChar currentChar
121  // ^
122  // position
123  //
124  // will be converted to
125  // .. .. GA .. ..
126 
127  sal_Int32 *p = nullptr;
128  sal_Int32 position = 0;
129  if (pOffset) {
130  // Allocate nCount length to offset argument.
131  pOffset->realloc( nCount );
132  p = pOffset->getArray();
133  position = startPos;
134  }
135 
136  //
137  sal_Unicode previousChar = *src ++;
138  sal_Unicode currentChar;
139 
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 ?
150 
151  if (2 <= j && j <= 3) // 0x309b or 0x309c
152  j -= 2;
153 
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;
159 
160  if (0 <= i && i <= (0x30ff - 0x3040) && composition_table[i][j])
161  bCompose = true;
162 
163  // not to use combined KATAKANA LETTER VU
164  if ( previousChar == 0x30a6 && (nFlags & WIDTHFOLDING_DONT_USE_COMBINED_VU) )
165  bCompose = false;
166 
167  if( bCompose ){
168  if (pOffset) {
169  position ++;
170  *p ++ = position ++;
171  }
172  *dst ++ = composition_table[i][j];
173  previousChar = *src ++;
174  nCount --;
175  continue;
176  }
177  }
178  if (pOffset)
179  *p ++ = position ++;
180  *dst ++ = previousChar;
181  previousChar = currentChar;
182  }
183 
184  if (nCount == 0) {
185  if (pOffset)
186  *p = position;
187  *dst ++ = previousChar;
188  }
189 
190  *dst = u'\0';
191 
192  newStr->length = sal_Int32(dst - newStr->buffer);
193  }
194  if (pOffset)
195  pOffset->realloc(newStr->length);
196  return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
197 }
198 
199 oneToOneMapping& widthfolding::gethalf2fullTable()
200 {
201  static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_NORMAL);
202  table.makeIndex();
203  return table;
204 }
205 
206 sal_Unicode widthfolding::getCompositionChar(sal_Unicode c1, sal_Unicode c2)
207 {
208  return composition_table[c1 - 0x3040][c2 - 0x3099];
209 }
210 
211 
212 oneToOneMapping& widthfolding::getfull2halfTableForASC()
213 {
214  static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_ASC_FUNCTION);
215  table.makeIndex();
216 
217  return table;
218 }
219 
220 oneToOneMapping& widthfolding::gethalf2fullTableForJIS()
221 {
222  static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_JIS_FUNCTION);
223  table.makeIndex();
224 
225  return table;
226 }
227 
228 oneToOneMapping& widthfolding::getfullKana2halfKanaTable()
229 {
230  static oneToOneMappingWithFlag table(full2half, sizeof(full2half), FULL2HALF_KATAKANA_ONLY);
231  table.makeIndex();
232  return table;
233 }
234 
235 oneToOneMapping& widthfolding::gethalfKana2fullKanaTable()
236 {
237  static oneToOneMappingWithFlag table(half2full, sizeof(half2full), HALF2FULL_KATAKANA_ONLY);
238  table.makeIndex();
239  return table;
240 }
241 
242 }
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define WIDTHFOLDING_DONT_USE_COMBINED_VU
#define HALF2FULL_KATAKANA_ONLY
const decomposition_table_entry_t decomposition_table[]
UnicodePairWithFlag const half2full[]
#define FULL2HALF_ASC_FUNCTION
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
exports com.sun.star. table
def position
sal_uInt16 sal_Unicode
#define HALF2FULL_NORMAL
#define HALF2FULL_JIS_FUNCTION
int i
float u
const sal_Unicode composition_table[][2]
#define FULL2HALF_NORMAL
void * p
UnicodePairWithFlag const full2half[]
sal_Unicode decomposited_character_1
constexpr OUStringLiteral first
#define FULL2HALF_KATAKANA_ONLY
sal_Unicode decomposited_character_2