1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: breakit.cxx,v $
10 * $Revision: 1.16.112.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include "breakit.hxx"
35 #include <unicode/uchar.h>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #ifndef _COM_SUN_STAR_I18N_SCRIPTTYPE_HDL_
38 #include <com/sun/star/i18n/ScriptType.hdl>
40 #include <unotools/localedatawrapper.hxx>
42 #ifndef _SVX_LINGU_HXX
43 #include <svx/unolingu.hxx>
45 #include <svx/scripttypeitem.hxx>
46 #include "swtypes.hxx"
48 using namespace com::sun::star
;
50 SwBreakIt
* pBreakIt
= 0;
52 void SwBreakIt::_Create(
53 const uno::Reference
< lang::XMultiServiceFactory
> & rxMSF
)
55 delete pBreakIt
, pBreakIt
= new SwBreakIt( rxMSF
);
58 void SwBreakIt::_Delete()
60 delete pBreakIt
, pBreakIt
= 0;
63 SwBreakIt
* SwBreakIt::Get()
69 const uno::Reference
< lang::XMultiServiceFactory
> & rxMSF
)
73 aLast( LANGUAGE_DONTKNOW
),
74 aForbiddenLang( LANGUAGE_DONTKNOW
)
76 DBG_ASSERT( m_xMSF
.is(), "SwBreakIt: no MultiServiceFactory" );
79 // xBreak = uno::Reference< i18n::XBreakIterator >(
80 // m_xMSF->createInstance(
81 // rtl::OUString::createFromAscii( "com.sun.star.i18n.BreakIterator" ) ),
84 // xCTLDetect = uno::Reference< i18n::XScriptTypeDetector >(
85 // m_xMSF->createInstance(
86 // rtl::OUString::createFromAscii( "com.sun.star.i18n.ScriptTypeDetector" ) ),
91 SwBreakIt::~SwBreakIt()
96 void SwBreakIt::createBreakIterator() const
98 if ( m_xMSF
.is() && !xBreak
.is() )
99 xBreak
.set(m_xMSF
->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.i18n.BreakIterator"))),uno::UNO_QUERY
);
101 void SwBreakIt::createScriptTypeDetector()
103 if ( m_xMSF
.is() && !xCTLDetect
.is() )
104 xCTLDetect
.set(m_xMSF
->createInstance(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.ScriptTypeDetector" ))),uno::UNO_QUERY
);
106 void SwBreakIt::_GetLocale( const LanguageType aLang
)
110 m_pLocale
= new lang::Locale( SvxCreateLocale( aLast
) );
113 void SwBreakIt::_GetForbidden( const LanguageType aLang
)
115 LocaleDataWrapper
aWrap( m_xMSF
, GetLocale( aLang
) );
117 aForbiddenLang
= aLang
;
119 m_pForbidden
= new i18n::ForbiddenCharacters( aWrap
.getForbiddenCharacters() );
122 USHORT
SwBreakIt::GetRealScriptOfText( const String
& rTxt
,
123 xub_StrLen nPos
) const
125 createBreakIterator();
126 USHORT nScript
= i18n::ScriptType::WEAK
;
127 if( xBreak
.is() && rTxt
.Len() )
129 if( nPos
&& nPos
== rTxt
.Len() )
131 nScript
= xBreak
->getScriptType( rTxt
, nPos
);
132 sal_Int32 nChgPos
= 0;
133 if ( i18n::ScriptType::WEAK
== nScript
&& nPos
+ 1 < rTxt
.Len() )
135 // A weak character followed by a mark may be meant to combine with
136 // the mark, so prefer the following character's script
137 switch ( u_charType(rTxt
.GetChar(nPos
+ 1) ) ) {
138 case U_NON_SPACING_MARK
:
139 case U_ENCLOSING_MARK
:
140 case U_COMBINING_SPACING_MARK
:
141 nScript
= xBreak
->getScriptType( rTxt
, nPos
+1 );
145 if( i18n::ScriptType::WEAK
== nScript
&& nPos
&&
146 0 < (nChgPos
= xBreak
->beginOfScript( rTxt
, nPos
, nScript
)) )
147 nScript
= xBreak
->getScriptType( rTxt
, nChgPos
-1 );
149 if( i18n::ScriptType::WEAK
== nScript
&& rTxt
.Len() >
150 ( nChgPos
= xBreak
->endOfScript( rTxt
, nPos
, nScript
) ) &&
152 nScript
= xBreak
->getScriptType( rTxt
, nChgPos
);
154 if( i18n::ScriptType::WEAK
== nScript
)
155 nScript
= GetI18NScriptTypeOfLanguage( (USHORT
)GetAppLanguage() );
159 USHORT
SwBreakIt::GetAllScriptsOfText( const String
& rTxt
) const
161 const USHORT coAllScripts
= ( SCRIPTTYPE_LATIN
|
163 SCRIPTTYPE_COMPLEX
);
164 createBreakIterator();
165 USHORT nRet
= 0, nScript
;
168 else if( rTxt
.Len() )
170 for( xub_StrLen n
= 0, nEnd
= rTxt
.Len(); n
< nEnd
;
171 n
= static_cast<xub_StrLen
>(xBreak
->endOfScript( rTxt
, n
, nScript
)) )
173 switch( nScript
= xBreak
->getScriptType( rTxt
, n
) )
175 case i18n::ScriptType::LATIN
: nRet
|= SCRIPTTYPE_LATIN
; break;
176 case i18n::ScriptType::ASIAN
: nRet
|= SCRIPTTYPE_ASIAN
; break;
177 case i18n::ScriptType::COMPLEX
: nRet
|= SCRIPTTYPE_COMPLEX
; break;
178 case i18n::ScriptType::WEAK
:
180 nRet
|= coAllScripts
;
183 if( coAllScripts
== nRet
)