merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / bastyp / breakit.cxx
blob9541707ea9a72d2de49b77385def9fa82f0a6faf
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>
39 #endif
40 #include <unotools/localedatawrapper.hxx>
42 #ifndef _SVX_LINGU_HXX
43 #include <svx/unolingu.hxx>
44 #endif
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()
65 return pBreakIt;
68 SwBreakIt::SwBreakIt(
69 const uno::Reference< lang::XMultiServiceFactory > & rxMSF)
70 : m_xMSF( rxMSF ),
71 m_pLocale( NULL ),
72 m_pForbidden( NULL ),
73 aLast( LANGUAGE_DONTKNOW ),
74 aForbiddenLang( LANGUAGE_DONTKNOW)
76 DBG_ASSERT( m_xMSF.is(), "SwBreakIt: no MultiServiceFactory" );
77 //if ( m_xMSF.is() )
78 //{
79 // xBreak = uno::Reference< i18n::XBreakIterator >(
80 // m_xMSF->createInstance(
81 // rtl::OUString::createFromAscii( "com.sun.star.i18n.BreakIterator" ) ),
82 // uno::UNO_QUERY);
84 // xCTLDetect = uno::Reference< i18n::XScriptTypeDetector >(
85 // m_xMSF->createInstance(
86 // rtl::OUString::createFromAscii( "com.sun.star.i18n.ScriptTypeDetector" ) ),
87 // uno::UNO_QUERY);
88 // }
91 SwBreakIt::~SwBreakIt()
93 delete m_pLocale;
94 delete m_pForbidden;
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 )
108 aLast = aLang;
109 delete m_pLocale;
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;
118 delete m_pForbidden;
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() )
130 --nPos;
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 );
142 break;
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 ) ) &&
151 0 <= nChgPos )
152 nScript = xBreak->getScriptType( rTxt, nChgPos );
154 if( i18n::ScriptType::WEAK == nScript )
155 nScript = GetI18NScriptTypeOfLanguage( (USHORT)GetAppLanguage() );
156 return nScript;
159 USHORT SwBreakIt::GetAllScriptsOfText( const String& rTxt ) const
161 const USHORT coAllScripts = ( SCRIPTTYPE_LATIN |
162 SCRIPTTYPE_ASIAN |
163 SCRIPTTYPE_COMPLEX );
164 createBreakIterator();
165 USHORT nRet = 0, nScript;
166 if( !xBreak.is() )
167 nRet = coAllScripts;
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:
179 if( !nRet )
180 nRet |= coAllScripts;
181 break;
183 if( coAllScripts == nRet )
184 break;
187 return nRet;