LCOV - code coverage report
Current view: top level - sw/source/core/bastyp - breakit.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 87 0.0 %
Date: 2014-04-14 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

          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 "breakit.hxx"
      21             : #include "swtypes.hxx"
      22             : 
      23             : #include <com/sun/star/i18n/ScriptType.hpp>
      24             : #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
      25             : #include <com/sun/star/i18n/BreakIterator.hpp>
      26             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      27             : #include <editeng/unolingu.hxx>
      28             : #include <editeng/scripttypeitem.hxx>
      29             : #include <unicode/uchar.h>
      30             : #include <unotools/localedatawrapper.hxx>
      31             : #include <comphelper/processfactory.hxx>
      32             : 
      33             : using namespace com::sun::star;
      34             : 
      35             : SwBreakIt* g_pBreakIt = 0;
      36             : 
      37           0 : void SwBreakIt::_Create( const uno::Reference<uno::XComponentContext> & rxContext )
      38             : {
      39           0 :     delete g_pBreakIt, g_pBreakIt = new SwBreakIt( rxContext );
      40           0 : }
      41             : 
      42           0 : void SwBreakIt::_Delete()
      43             : {
      44           0 :     delete g_pBreakIt, g_pBreakIt = 0;
      45           0 : }
      46             : 
      47           0 : SwBreakIt * SwBreakIt::Get()
      48             : {
      49           0 :     return g_pBreakIt;
      50             : }
      51             : 
      52           0 : SwBreakIt::SwBreakIt( const uno::Reference<uno::XComponentContext> & rxContext )
      53             :     : m_xContext( rxContext ),
      54             :       m_pLanguageTag( NULL ),
      55             :       m_pForbidden( NULL ),
      56           0 :       aForbiddenLang( LANGUAGE_DONTKNOW )
      57             : {
      58             :     OSL_ENSURE( m_xContext.is(), "SwBreakIt: no MultiServiceFactory" );
      59           0 : }
      60             : 
      61           0 : SwBreakIt::~SwBreakIt()
      62             : {
      63           0 :     delete m_pLanguageTag;
      64           0 :     delete m_pForbidden;
      65           0 : }
      66             : 
      67           0 : void SwBreakIt::createBreakIterator() const
      68             : {
      69           0 :     if ( m_xContext.is() && !xBreak.is() )
      70           0 :         xBreak.set( i18n::BreakIterator::create(m_xContext) );
      71           0 : }
      72             : 
      73           0 : void SwBreakIt::_GetLocale( const LanguageType aLang )
      74             : {
      75           0 :     if (m_pLanguageTag)
      76           0 :         m_pLanguageTag->reset( aLang );
      77             :     else
      78           0 :         m_pLanguageTag = new LanguageTag( aLang );
      79           0 : }
      80             : 
      81           0 : void SwBreakIt::_GetLocale( const LanguageTag& rLanguageTag )
      82             : {
      83           0 :     if (m_pLanguageTag)
      84           0 :         *m_pLanguageTag = rLanguageTag;
      85             :     else
      86           0 :         m_pLanguageTag = new LanguageTag( rLanguageTag );
      87           0 : }
      88             : 
      89           0 : void SwBreakIt::_GetForbidden( const LanguageType aLang )
      90             : {
      91           0 :     LocaleDataWrapper aWrap( m_xContext, GetLanguageTag( aLang ) );
      92             : 
      93           0 :     aForbiddenLang = aLang;
      94           0 :     delete m_pForbidden;
      95           0 :     m_pForbidden = new i18n::ForbiddenCharacters( aWrap.getForbiddenCharacters() );
      96           0 : }
      97             : 
      98           0 : sal_uInt16 SwBreakIt::GetRealScriptOfText( const OUString& rTxt, sal_Int32 nPos ) const
      99             : {
     100           0 :     createBreakIterator();
     101           0 :     sal_uInt16 nScript = i18n::ScriptType::WEAK;
     102           0 :     if( xBreak.is() && !rTxt.isEmpty() )
     103             :     {
     104           0 :         if( nPos && nPos == rTxt.getLength() )
     105           0 :             --nPos;
     106           0 :         nScript = xBreak->getScriptType( rTxt, nPos );
     107           0 :         sal_Int32 nChgPos = 0;
     108           0 :         if ( i18n::ScriptType::WEAK == nScript && nPos + 1 < rTxt.getLength() )
     109             :         {
     110             :             // A weak character followed by a mark may be meant to combine with
     111             :             // the mark, so prefer the following character's script
     112           0 :             switch (u_charType(rTxt[nPos + 1]))
     113             :             {
     114             :                 case U_NON_SPACING_MARK:
     115             :                 case U_ENCLOSING_MARK:
     116             :                 case U_COMBINING_SPACING_MARK:
     117           0 :                     nScript = xBreak->getScriptType( rTxt, nPos+1 );
     118           0 :                     break;
     119             :             }
     120             :         }
     121           0 :         if( i18n::ScriptType::WEAK == nScript &&
     122           0 :             nPos &&
     123           0 :             0 < ( nChgPos = xBreak->beginOfScript( rTxt, nPos, nScript ) ) )
     124             :         {
     125           0 :             nScript = xBreak->getScriptType( rTxt, nChgPos-1 );
     126             :         }
     127             : 
     128           0 :         if( i18n::ScriptType::WEAK == nScript &&
     129           0 :             rTxt.getLength() > ( nChgPos = xBreak->endOfScript( rTxt, nPos, nScript ) ) &&
     130             :             0 <= nChgPos )
     131             :         {
     132           0 :             nScript = xBreak->getScriptType( rTxt, nChgPos );
     133             :         }
     134             :     }
     135           0 :     if( i18n::ScriptType::WEAK == nScript )
     136           0 :         nScript = GetI18NScriptTypeOfLanguage( (sal_uInt16)GetAppLanguage() );
     137           0 :     return nScript;
     138             : }
     139             : 
     140           0 : sal_uInt16 SwBreakIt::GetAllScriptsOfText( const OUString& rTxt ) const
     141             : {
     142             :     const sal_uInt16 coAllScripts = ( SCRIPTTYPE_LATIN |
     143             :                                       SCRIPTTYPE_ASIAN |
     144           0 :                                       SCRIPTTYPE_COMPLEX );
     145           0 :     createBreakIterator();
     146           0 :     sal_uInt16 nRet = 0, nScript;
     147           0 :     if( !xBreak.is() )
     148             :     {
     149           0 :         nRet = coAllScripts;
     150             :     }
     151           0 :     else if( !rTxt.isEmpty() )
     152             :     {
     153           0 :         for( sal_Int32 n = 0, nEnd = rTxt.getLength(); n < nEnd;
     154           0 :                 n = xBreak->endOfScript(rTxt, n, nScript) )
     155             :         {
     156           0 :             switch( nScript = xBreak->getScriptType( rTxt, n ) )
     157             :             {
     158           0 :             case i18n::ScriptType::LATIN:   nRet |= SCRIPTTYPE_LATIN;   break;
     159           0 :             case i18n::ScriptType::ASIAN:   nRet |= SCRIPTTYPE_ASIAN;   break;
     160           0 :             case i18n::ScriptType::COMPLEX: nRet |= SCRIPTTYPE_COMPLEX; break;
     161             :             case i18n::ScriptType::WEAK:
     162           0 :                     if( !nRet )
     163           0 :                         nRet |= coAllScripts;
     164           0 :                     break;
     165             :             }
     166           0 :             if( coAllScripts == nRet )
     167           0 :                 break;
     168             :         }
     169             :     }
     170           0 :     return nRet;
     171             : }
     172             : 
     173           0 : sal_Int32 SwBreakIt::getGraphemeCount(const OUString& rText,
     174             :                                       sal_Int32 nStart, sal_Int32 nEnd) const
     175             : {
     176           0 :     sal_Int32 nGraphemeCount = 0;
     177             : 
     178           0 :     sal_Int32 nCurPos = nStart;
     179           0 :     while (nCurPos < nEnd)
     180             :     {
     181             :         // fdo#49208 cheat and assume that nothing can combine with a space
     182             :         // to form a single grapheme
     183           0 :         if (rText[nCurPos] == ' ')
     184             :         {
     185           0 :             ++nCurPos;
     186             :         }
     187             :         else
     188             :         {
     189           0 :             sal_Int32 nCount2 = 1;
     190           0 :             nCurPos = xBreak->nextCharacters(rText, nCurPos, lang::Locale(),
     191           0 :                 i18n::CharacterIteratorMode::SKIPCELL, nCount2, nCount2);
     192             :         }
     193           0 :         ++nGraphemeCount;
     194             :     }
     195             : 
     196           0 :     return nGraphemeCount;
     197             : }
     198             : 
     199             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10