Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / i18npool / source / breakiterator / breakiterator_cjk.cxx
blob88ea6efb29d901fc61683ce3b58c86f672bb01dd
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 <breakiterator_cjk.hxx>
21 #include <localedata.hxx>
22 #include <i18nutil/unicode.hxx>
24 using namespace ::com::sun::star::uno;
25 using namespace ::com::sun::star::lang;
27 namespace com { namespace sun { namespace star { namespace i18n {
29 // ----------------------------------------------------
30 // class BreakIterator_CJK
31 // ----------------------------------------------------;
33 BreakIterator_CJK::BreakIterator_CJK() :
34 dict( nullptr ),
35 hangingCharacters()
37 cBreakIterator = "com.sun.star.i18n.BreakIterator_CJK";
40 Boundary SAL_CALL
41 BreakIterator_CJK::previousWord(const OUString& text, sal_Int32 anyPos,
42 const lang::Locale& nLocale, sal_Int16 wordType) throw(RuntimeException, std::exception)
44 if (dict) {
45 result = dict->previousWord(text, anyPos, wordType);
46 // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
47 if (result.endPos - result.startPos != 1 ||
48 getScriptType(text, result.startPos) == ScriptType::ASIAN)
49 return result;
50 result = BreakIterator_Unicode::getWordBoundary(text, result.startPos, nLocale, wordType, true);
51 if (result.endPos < anyPos)
52 return result;
54 return BreakIterator_Unicode::previousWord(text, anyPos, nLocale, wordType);
57 Boundary SAL_CALL
58 BreakIterator_CJK::nextWord(const OUString& text, sal_Int32 anyPos,
59 const lang::Locale& nLocale, sal_Int16 wordType) throw(RuntimeException, std::exception)
61 if (dict) {
62 result = dict->nextWord(text, anyPos, wordType);
63 // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
64 if (result.endPos - result.startPos != 1 ||
65 getScriptType(text, result.startPos) == ScriptType::ASIAN)
66 return result;
67 result = BreakIterator_Unicode::getWordBoundary(text, result.startPos, nLocale, wordType, true);
68 if (result.startPos > anyPos)
69 return result;
71 return BreakIterator_Unicode::nextWord(text, anyPos, nLocale, wordType);
74 Boundary SAL_CALL
75 BreakIterator_CJK::getWordBoundary( const OUString& text, sal_Int32 anyPos,
76 const lang::Locale& nLocale, sal_Int16 wordType, sal_Bool bDirection )
77 throw(RuntimeException, std::exception)
79 if (dict) {
80 result = dict->getWordBoundary(text, anyPos, wordType, bDirection);
81 // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
82 if (result.endPos - result.startPos != 1 ||
83 getScriptType(text, result.startPos) == ScriptType::ASIAN)
84 return result;
86 return BreakIterator_Unicode::getWordBoundary(text, anyPos, nLocale, wordType, bDirection);
89 LineBreakResults SAL_CALL BreakIterator_CJK::getLineBreak(
90 const OUString& Text, sal_Int32 nStartPos,
91 const lang::Locale& /*rLocale*/, sal_Int32 /*nMinBreakPos*/,
92 const LineBreakHyphenationOptions& /*hOptions*/,
93 const LineBreakUserOptions& bOptions ) throw(RuntimeException, std::exception)
95 LineBreakResults lbr;
97 if (bOptions.allowPunctuationOutsideMargin &&
98 hangingCharacters.indexOf(Text[nStartPos]) != -1 &&
99 (Text.iterateCodePoints( &nStartPos ), nStartPos == Text.getLength())) {
100 ; // do nothing
101 } else if (bOptions.applyForbiddenRules && 0 < nStartPos && nStartPos < Text.getLength()) {
102 while (nStartPos > 0 &&
103 (bOptions.forbiddenBeginCharacters.indexOf(Text[nStartPos]) != -1 ||
104 bOptions.forbiddenEndCharacters.indexOf(Text[nStartPos-1]) != -1))
105 Text.iterateCodePoints( &nStartPos, -1);
108 lbr.breakIndex = nStartPos;
109 lbr.breakType = BreakType::WORDBOUNDARY;
110 return lbr;
113 #define LOCALE(language, country) lang::Locale(language, country, OUString())
114 // ----------------------------------------------------
115 // class BreakIterator_zh
116 // ----------------------------------------------------;
117 BreakIterator_zh::BreakIterator_zh()
119 dict = new xdictionary("zh");
120 hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("zh", "CN"));
121 cBreakIterator = "com.sun.star.i18n.BreakIterator_zh";
124 BreakIterator_zh::~BreakIterator_zh()
126 delete dict;
129 // ----------------------------------------------------
130 // class BreakIterator_zh_TW
131 // ----------------------------------------------------;
132 BreakIterator_zh_TW::BreakIterator_zh_TW()
134 dict = new xdictionary("zh");
135 hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("zh", "TW"));
136 cBreakIterator = "com.sun.star.i18n.BreakIterator_zh_TW";
139 BreakIterator_zh_TW::~BreakIterator_zh_TW()
141 delete dict;
144 // ----------------------------------------------------
145 // class BreakIterator_ja
146 // ----------------------------------------------------;
147 BreakIterator_ja::BreakIterator_ja()
149 dict = new xdictionary("ja");
150 dict->setJapaneseWordBreak();
151 hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("ja", "JP"));
152 cBreakIterator = "com.sun.star.i18n.BreakIterator_ja";
155 BreakIterator_ja::~BreakIterator_ja()
157 delete dict;
160 // ----------------------------------------------------
161 // class BreakIterator_ko
162 // ----------------------------------------------------;
163 BreakIterator_ko::BreakIterator_ko()
165 hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("ko", "KR"));
166 cBreakIterator = "com.sun.star.i18n.BreakIterator_ko";
169 BreakIterator_ko::~BreakIterator_ko()
173 } } } }
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */