merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / breakiterator / breakiterator_ctl.cxx
blob16743cdbb6668f32b79e4fd0ce486b278efd61ae
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: breakiterator_ctl.cxx,v $
10 * $Revision: 1.14 $
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_i18npool.hxx"
34 #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
35 #include <breakiterator_ctl.hxx>
37 #include <string.h> // for memset
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::rtl;
43 namespace com { namespace sun { namespace star { namespace i18n {
45 /**
46 * Constructor.
48 BreakIterator_CTL::BreakIterator_CTL() :
49 cachedText(),
50 nextCellIndex( NULL ),
51 previousCellIndex( NULL ),
52 cellIndexSize( 512 )
54 cBreakIterator = "com.sun.star.i18n.BreakIterator_CTL";
55 // to improve performance, alloc big enough memory in construct.
56 nextCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
57 previousCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
58 memset(nextCellIndex, 0, cellIndexSize * sizeof(sal_Int32));
61 /**
62 * Deconstructor.
64 BreakIterator_CTL::~BreakIterator_CTL()
66 free(nextCellIndex);
67 free(previousCellIndex);
70 sal_Int32 SAL_CALL BreakIterator_CTL::previousCharacters( const OUString& Text,
71 sal_Int32 nStartPos, const lang::Locale& rLocale,
72 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
73 throw(RuntimeException)
75 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
76 nDone = 0;
77 if (nStartPos > 0) { // for others to skip cell.
78 makeIndex(Text, nStartPos);
80 if (nextCellIndex[nStartPos-1] == 0) // not a CTL character
81 return BreakIterator_Unicode::previousCharacters(Text, nStartPos, rLocale,
82 nCharacterIteratorMode, nCount, nDone);
83 else while (nCount > 0 && nextCellIndex[nStartPos - 1] > 0) {
84 nCount--; nDone++;
85 nStartPos = previousCellIndex[nStartPos - 1];
87 } else
88 nStartPos = 0;
89 } else { // for BS to delete one char.
90 nDone = (nStartPos > nCount) ? nCount : nStartPos;
91 nStartPos -= nDone;
94 return nStartPos;
97 sal_Int32 SAL_CALL BreakIterator_CTL::nextCharacters(const OUString& Text,
98 sal_Int32 nStartPos, const lang::Locale& rLocale,
99 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone)
100 throw(RuntimeException)
102 sal_Int32 len = Text.getLength();
103 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
104 nDone = 0;
105 if (nStartPos < len) {
106 makeIndex(Text, nStartPos);
108 if (nextCellIndex[nStartPos] == 0) // not a CTL character
109 return BreakIterator_Unicode::nextCharacters(Text, nStartPos, rLocale,
110 nCharacterIteratorMode, nCount, nDone);
111 else while (nCount > 0 && nextCellIndex[nStartPos] > 0) {
112 nCount--; nDone++;
113 nStartPos = nextCellIndex[nStartPos];
115 } else
116 nStartPos = len;
117 } else {
118 nDone = (len - nStartPos > nCount) ? nCount : len - nStartPos;
119 nStartPos += nDone;
122 return nStartPos;
125 // This method should be overwritten by derived language specific class.
126 void SAL_CALL BreakIterator_CTL::makeIndex(const OUString& /*text*/, sal_Int32 /*pos*/)
127 throw(RuntimeException)
129 throw RuntimeException();
132 // Make sure line is broken on cell boundary if we implement cell iterator.
133 LineBreakResults SAL_CALL BreakIterator_CTL::getLineBreak(
134 const OUString& Text, sal_Int32 nStartPos,
135 const lang::Locale& rLocale, sal_Int32 nMinBreakPos,
136 const LineBreakHyphenationOptions& hOptions,
137 const LineBreakUserOptions& bOptions ) throw(RuntimeException)
139 LineBreakResults lbr = BreakIterator_Unicode::getLineBreak(Text, nStartPos,
140 rLocale, nMinBreakPos, hOptions, bOptions );
141 if (lbr.breakIndex < Text.getLength()) {
142 makeIndex(Text, lbr.breakIndex);
143 lbr.breakIndex = previousCellIndex[ lbr.breakIndex ];
145 return lbr;
148 } } } }