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