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 : #ifndef SVX_HANGUL_HANJA_CONVERSION_HXX
20 : #define SVX_HANGUL_HANJA_CONVERSION_HXX
21 :
22 : #include <vcl/window.hxx>
23 : #include <memory>
24 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 : #include <com/sun/star/lang/Locale.hpp>
26 : #include <com/sun/star/uno/Sequence.hxx>
27 : #include "editeng/editengdllapi.h"
28 : #include <i18npool/mslangid.hxx>
29 :
30 : //.............................................................................
31 : namespace editeng
32 : {
33 : //.............................................................................
34 :
35 : class HangulHanjaConversion_Impl;
36 :
37 : //=========================================================================
38 : //= HangulHanjaConversion
39 : //=========================================================================
40 : /** encapsulates Hangul-Hanja conversion functionality
41 :
42 : <p>terminology:
43 : <ul><li>A <b>text <em>portion</em></b> is some (potentially large) piece of text
44 : which is to be analyzed for convertible sub-strings.</li>
45 : <li>A <b>text <em>unit</em></b> is a sub string in a text portion, which is
46 : to be converted as a whole.</li>
47 : </ul>
48 : For instance, you could have two independent selections within your document, which are then
49 : two text portions. A text unit would be single Hangul/Hanja words within a portion, or even
50 : single Hangul syllabills when "replace by character" is enabled.
51 : </p>
52 : */
53 : class EDITENG_DLLPUBLIC HangulHanjaConversion
54 : {
55 : friend class HangulHanjaConversion_Impl;
56 :
57 : public:
58 : enum ReplacementAction
59 : {
60 : eExchange, // simply exchange one text with another
61 : eReplacementBracketed, // keep the original, and put the replacement in brackets after it
62 : eOriginalBracketed, // replace the original text, but put it in brackeds after the replacement
63 : eReplacementAbove, // keep the original, and put the replacement text as ruby text above it
64 : eOriginalAbove, // replace the original text, but put it as ruby text above it
65 : eReplacementBelow, // keep the original, and put the replacement text as ruby text below it
66 : eOriginalBelow // replace the original text, but put it as ruby text below it
67 : };
68 :
69 : enum ConversionType // does not specify direction...
70 : {
71 : eConvHangulHanja, // Korean Hangul/Hanja conversion
72 : eConvSimplifiedTraditional // Chinese simplified / Chinese traditional conversion
73 : };
74 :
75 : // Note: conversion direction for eConvSimplifiedTraditional is
76 : // specified by source language.
77 : // This one is for Hangul/Hanja where source and target language
78 : // are the same.
79 : enum ConversionDirection
80 : {
81 : eHangulToHanja,
82 : eHanjaToHangul
83 : };
84 :
85 : enum ConversionFormat
86 : {
87 : eSimpleConversion, // used for simplified / traditional Chinese as well
88 : eHangulBracketed,
89 : eHanjaBracketed,
90 : eRubyHanjaAbove,
91 : eRubyHanjaBelow,
92 : eRubyHangulAbove,
93 : eRubyHangulBelow
94 : };
95 :
96 : private:
97 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
98 : ::std::auto_ptr< HangulHanjaConversion_Impl > m_pImpl;
99 : SAL_WNODEPRECATED_DECLARATIONS_POP
100 :
101 : // used to set initial values of m_pImpl object from saved ones
102 : static sal_Bool m_bUseSavedValues; // defines if the followng two values should be used for initialization
103 : static sal_Bool m_bTryBothDirectionsSave;
104 : static ConversionDirection m_ePrimaryConversionDirectionSave;
105 :
106 : // Forbidden and not implemented.
107 : HangulHanjaConversion (const HangulHanjaConversion &);
108 : HangulHanjaConversion & operator= (const HangulHanjaConversion &);
109 :
110 : public:
111 : HangulHanjaConversion(
112 : Window* _pUIParent,
113 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
114 : const ::com::sun::star::lang::Locale& _rSourceLocale,
115 : const ::com::sun::star::lang::Locale& _rTargetLocale,
116 : const Font* _pTargetFont,
117 : sal_Int32 nOptions,
118 : sal_Bool _bIsInteractive
119 : );
120 :
121 : virtual ~HangulHanjaConversion( );
122 :
123 : // converts the whole document
124 : void ConvertDocument();
125 :
126 : LanguageType GetSourceLanguage() const;
127 : LanguageType GetTargetLanguage() const;
128 : const Font * GetTargetFont() const;
129 : sal_Int32 GetConversionOptions() const;
130 : sal_Bool IsInteractive() const;
131 :
132 : // chinese text conversion
133 : static inline sal_Bool IsSimplified( LanguageType nLang );
134 : static inline sal_Bool IsTraditional( LanguageType nLang );
135 : static inline sal_Bool IsChinese( LanguageType nLang );
136 :
137 : // used to specify that the conversion direction states from the
138 : // last incarnation should be used as
139 : // initial conversion direction for the next incarnation.
140 : // (A hack used to transport a state information from
141 : // one incarnation to the next. Used in Writers text conversion...)
142 : static void SetUseSavedConversionDirectionState( sal_Bool bVal );
143 : static sal_Bool IsUseSavedConversionDirectionState();
144 :
145 : protected:
146 : /** retrieves the next text portion which is to be analyzed
147 :
148 : <p>pseudo-abstract, needs to be overridden</p>
149 :
150 : @param _rNextPortion
151 : upon return, this must contain the next text portion
152 : @param _rLangOfPortion
153 : upon return, this must contain the language for the found text portion.
154 : (necessary for Chinese translation since there are 5 language variants
155 : too look for even if the 'source' language usually is only 'simplified'
156 : or 'traditional'.)
157 : */
158 : virtual void GetNextPortion(
159 : ::rtl::OUString& /* [out] */ _rNextPortion,
160 : LanguageType& /* [out] */ _rLangOfPortion,
161 : sal_Bool /* [in] */ _bAllowImplicitChangesForNotConvertibleText ) = 0;
162 :
163 : /** announces a new "current unit"
164 :
165 : <p>This will be called whenever it is necessary to interactively ask the user for
166 : a conversion. In such a case, a range within the current portion (see <member>GetNextPortion</member>)
167 : is presented to the user for chosing a substitution. Additionally, this method is called,
168 : so that derived classes can e.g. highlight this text range in a document view.</p>
169 :
170 : <p>Note that the indexes are relative to the most recent replace action. See
171 : <member>ReplaceUnit</member> for details.</p>
172 :
173 : @param _nUnitStart
174 : the start index of the unit
175 :
176 : @param _nUnitEnd
177 : the start index (exclusively!) of the unit.
178 :
179 : @param _bAllowImplicitChangesForNotConvertibleText
180 : allows implicit changes other than the text itself for the
181 : text parts not being convertible.
182 : Used for chinese translation to attribute all not convertible
183 : text (e.g. western text, empty paragraphs, spaces, ...) to
184 : the target language and target font of the conversion.
185 : This is to ensure that after the conversion any new text entered
186 : anywhere in the document will have the target language (of course
187 : CJK Language only) and target font (CJK font only) set.
188 :
189 : @see GetNextPortion
190 : */
191 : virtual void HandleNewUnit( const sal_Int32 _nUnitStart, const sal_Int32 _nUnitEnd ) = 0;
192 :
193 : /** replaces a text unit within a text portion with a new text
194 :
195 : <p>pseudo-abstract, needs to be overridden</p>
196 :
197 : <p>Note an important thing about the indicies: They are always relative to the <em>previous
198 : call</em> of ReplaceUnit. This means whe you get a call to ReplaceUnit, and replace some text
199 : in your document, than you have to remember the document position immediately <em>behind</em>
200 : the changed text. In a next call to ReplaceUnit, an index of <em>0</em> will denote exactly
201 : this position behind the previous replacement<br/>
202 : The reaons for this is that this class here does not know anything about your document structure,
203 : so after a replacement took place, it's impossible to address anything in the range from the
204 : beginning of the portion up to the replaced text.<br/>
205 : In the very first call to ReplaceUnit, an index of <em>0</em> denotes the very first position of
206 : the current portion.</p>
207 :
208 : <p>If the language of the text to be replaced is different from
209 : the target language (as given by 'GetTargetLanguage') for example
210 : when converting simplified Chinese from/to traditional Chinese
211 : the language attribute of the new text has to be changed as well,
212 : **and** the font is to be set to the default (document) font for
213 : that language.</p>
214 :
215 : @param _nUnitStart
216 : the start index of the range to replace
217 :
218 : @param _nUnitEnd
219 : the end index (exclusively!) of the range to replace. E.g., an index
220 : pair (4,5) indicates a range of length 1.
221 :
222 : @param _rOrigText
223 : the original text to be replaced (as returned by GetNextPortion).
224 : Since in Chinese conversion the original text is needed as well
225 : in order to only do the minimal necassry text changes and to keep
226 : as much attributes as possible this is supplied here as well.
227 :
228 : @param _rReplaceWith
229 : The replacement text
230 :
231 : @param _rOffsets
232 : An sequence matching the indices (characters) of _rReplaceWith
233 : to the indices of the characters in the original text they are
234 : replacing.
235 : This is necessary since some portions of the text may get
236 : converted in portions of different length than the original.
237 : The sequence will be empty if all conversions in the text are
238 : of equal length. That is if always the character at index i in
239 : _rOffsets is replacing the character at index i in the original
240 : text for all valid index values of i.
241 :
242 : @param _eAction
243 : replacement action to take
244 :
245 : @param pNewUnitLanguage
246 : if the replacement unit is required to have a new language that
247 : is specified here. If the language is to be left unchanged this
248 : is the 0 pointer.
249 : */
250 : virtual void ReplaceUnit(
251 : const sal_Int32 _nUnitStart, const sal_Int32 _nUnitEnd,
252 : const ::rtl::OUString& _rOrigText,
253 : const ::rtl::OUString& _rReplaceWith,
254 : const ::com::sun::star::uno::Sequence< sal_Int32 > &_rOffsets,
255 : ReplacementAction _eAction,
256 : LanguageType *pNewUnitLanguage
257 : ) = 0;
258 :
259 : /** specifies if rubies are supported by the document implementing
260 : this class.
261 :
262 : @return
263 : <TRUE/> if rubies are supported.
264 : */
265 : virtual sal_Bool HasRubySupport() const = 0;
266 : };
267 :
268 0 : sal_Bool HangulHanjaConversion::IsSimplified( LanguageType nLang )
269 : {
270 0 : return MsLangId::isSimplifiedChinese(nLang);
271 : }
272 :
273 0 : sal_Bool HangulHanjaConversion::IsTraditional( LanguageType nLang )
274 : {
275 0 : return MsLangId::isTraditionalChinese(nLang);
276 : }
277 :
278 0 : sal_Bool HangulHanjaConversion::IsChinese( LanguageType nLang )
279 : {
280 0 : return MsLangId::isChinese(nLang);
281 : }
282 : //.............................................................................
283 : } // namespace svx
284 : //.............................................................................
285 :
286 : #endif // SVX_HANGUL_HANJA_CONVERSION_HXX
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|