Line data Source code
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 <breakiterator_cjk.hxx>
21 : #include <localedata.hxx>
22 : #include <i18nutil/unicode.hxx>
23 :
24 : using namespace ::com::sun::star::uno;
25 : using namespace ::com::sun::star::lang;
26 :
27 : namespace com { namespace sun { namespace star { namespace i18n {
28 :
29 : // ----------------------------------------------------
30 : // class BreakIterator_CJK
31 : // ----------------------------------------------------;
32 :
33 102 : BreakIterator_CJK::BreakIterator_CJK() :
34 : dict( NULL ),
35 102 : hangingCharacters()
36 : {
37 102 : cBreakIterator = "com.sun.star.i18n.BreakIterator_CJK";
38 102 : }
39 :
40 : Boundary SAL_CALL
41 0 : BreakIterator_CJK::previousWord(const OUString& text, sal_Int32 anyPos,
42 : const lang::Locale& nLocale, sal_Int16 wordType) throw(RuntimeException, std::exception)
43 : {
44 0 : if (dict) {
45 0 : result = dict->previousWord(text, anyPos, wordType);
46 : // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
47 0 : if (result.endPos - result.startPos != 1 ||
48 0 : getScriptType(text, result.startPos) == ScriptType::ASIAN)
49 0 : return result;
50 0 : result = BreakIterator_Unicode::getWordBoundary(text, result.startPos, nLocale, wordType, true);
51 0 : if (result.endPos < anyPos)
52 0 : return result;
53 : }
54 0 : return BreakIterator_Unicode::previousWord(text, anyPos, nLocale, wordType);
55 : }
56 :
57 : Boundary SAL_CALL
58 0 : BreakIterator_CJK::nextWord(const OUString& text, sal_Int32 anyPos,
59 : const lang::Locale& nLocale, sal_Int16 wordType) throw(RuntimeException, std::exception)
60 : {
61 0 : if (dict) {
62 0 : result = dict->nextWord(text, anyPos, wordType);
63 : // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
64 0 : if (result.endPos - result.startPos != 1 ||
65 0 : getScriptType(text, result.startPos) == ScriptType::ASIAN)
66 0 : return result;
67 0 : result = BreakIterator_Unicode::getWordBoundary(text, result.startPos, nLocale, wordType, true);
68 0 : if (result.startPos > anyPos)
69 0 : return result;
70 : }
71 0 : return BreakIterator_Unicode::nextWord(text, anyPos, nLocale, wordType);
72 : }
73 :
74 : Boundary SAL_CALL
75 102 : 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)
78 : {
79 102 : if (dict) {
80 102 : result = dict->getWordBoundary(text, anyPos, wordType, bDirection);
81 : // #109813# for non-CJK, single character word, fallback to ICU breakiterator.
82 172 : if (result.endPos - result.startPos != 1 ||
83 70 : getScriptType(text, result.startPos) == ScriptType::ASIAN)
84 102 : return result;
85 : }
86 0 : return BreakIterator_Unicode::getWordBoundary(text, anyPos, nLocale, wordType, bDirection);
87 : }
88 :
89 0 : 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)
94 : {
95 0 : LineBreakResults lbr;
96 :
97 0 : if (bOptions.allowPunctuationOutsideMargin &&
98 0 : hangingCharacters.indexOf(Text[nStartPos]) != -1 &&
99 0 : (Text.iterateCodePoints( &nStartPos, 1), nStartPos == Text.getLength())) {
100 : ; // do nothing
101 0 : } else if (bOptions.applyForbiddenRules && 0 < nStartPos && nStartPos < Text.getLength()) {
102 0 : while (nStartPos > 0 &&
103 0 : (bOptions.forbiddenBeginCharacters.indexOf(Text[nStartPos]) != -1 ||
104 0 : bOptions.forbiddenEndCharacters.indexOf(Text[nStartPos-1]) != -1))
105 0 : Text.iterateCodePoints( &nStartPos, -1);
106 : }
107 :
108 0 : lbr.breakIndex = nStartPos;
109 0 : lbr.breakType = BreakType::WORDBOUNDARY;
110 0 : return lbr;
111 : }
112 :
113 : #define LOCALE(language, country) lang::Locale(OUString::createFromAscii(language), OUString::createFromAscii(country), OUString())
114 : // ----------------------------------------------------
115 : // class BreakIterator_zh
116 : // ----------------------------------------------------;
117 80 : BreakIterator_zh::BreakIterator_zh()
118 : {
119 80 : dict = new xdictionary("zh");
120 80 : hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("zh", "CN"));
121 80 : cBreakIterator = "com.sun.star.i18n.BreakIterator_zh";
122 80 : }
123 :
124 240 : BreakIterator_zh::~BreakIterator_zh()
125 : {
126 80 : delete dict;
127 160 : }
128 :
129 : // ----------------------------------------------------
130 : // class BreakIterator_zh_TW
131 : // ----------------------------------------------------;
132 18 : BreakIterator_zh_TW::BreakIterator_zh_TW()
133 : {
134 18 : dict = new xdictionary("zh");
135 18 : hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("zh", "TW"));
136 18 : cBreakIterator = "com.sun.star.i18n.BreakIterator_zh_TW";
137 18 : }
138 :
139 54 : BreakIterator_zh_TW::~BreakIterator_zh_TW()
140 : {
141 18 : delete dict;
142 36 : }
143 :
144 : // ----------------------------------------------------
145 : // class BreakIterator_ja
146 : // ----------------------------------------------------;
147 4 : BreakIterator_ja::BreakIterator_ja()
148 : {
149 4 : dict = new xdictionary("ja");
150 4 : dict->setJapaneseWordBreak();
151 4 : hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("ja", "JP"));
152 4 : cBreakIterator = "com.sun.star.i18n.BreakIterator_ja";
153 4 : }
154 :
155 12 : BreakIterator_ja::~BreakIterator_ja()
156 : {
157 4 : delete dict;
158 8 : }
159 :
160 : // ----------------------------------------------------
161 : // class BreakIterator_ko
162 : // ----------------------------------------------------;
163 0 : BreakIterator_ko::BreakIterator_ko()
164 : {
165 0 : hangingCharacters = LocaleDataImpl().getHangingCharacters(LOCALE("ko", "KR"));
166 0 : cBreakIterator = "com.sun.star.i18n.BreakIterator_ko";
167 0 : }
168 :
169 0 : BreakIterator_ko::~BreakIterator_ko()
170 : {
171 0 : }
172 :
173 : } } } }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|