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 :
21 : #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
22 : #include <breakiterator_ctl.hxx>
23 :
24 : using namespace ::com::sun::star::uno;
25 : using namespace ::com::sun::star::lang;
26 : using namespace ::rtl;
27 :
28 : namespace com { namespace sun { namespace star { namespace i18n {
29 :
30 : /**
31 : * Constructor.
32 : */
33 0 : BreakIterator_CTL::BreakIterator_CTL() :
34 : cachedText(),
35 : nextCellIndex( NULL ),
36 : previousCellIndex( NULL ),
37 0 : cellIndexSize( 512 )
38 : {
39 0 : cBreakIterator = "com.sun.star.i18n.BreakIterator_CTL";
40 : // to improve performance, alloc big enough memory in construct.
41 0 : nextCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
42 0 : previousCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
43 0 : }
44 :
45 : /**
46 : * Deconstructor.
47 : */
48 0 : BreakIterator_CTL::~BreakIterator_CTL()
49 : {
50 0 : free(nextCellIndex);
51 0 : free(previousCellIndex);
52 0 : }
53 :
54 0 : sal_Int32 SAL_CALL BreakIterator_CTL::previousCharacters( const OUString& Text,
55 : sal_Int32 nStartPos, const lang::Locale& rLocale,
56 : sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
57 : throw(RuntimeException, std::exception)
58 : {
59 0 : if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
60 0 : nDone = 0;
61 0 : if (nStartPos > 0) { // for others to skip cell.
62 0 : makeIndex(Text, nStartPos);
63 :
64 0 : if (nextCellIndex[nStartPos-1] == 0) // not a CTL character
65 : return BreakIterator_Unicode::previousCharacters(Text, nStartPos, rLocale,
66 0 : nCharacterIteratorMode, nCount, nDone);
67 0 : else while (nCount > 0 && nextCellIndex[nStartPos - 1] > 0) {
68 0 : nCount--; nDone++;
69 0 : nStartPos = previousCellIndex[nStartPos - 1];
70 : }
71 : } else
72 0 : nStartPos = 0;
73 : } else { // for BS to delete one char.
74 0 : nDone = (nStartPos > nCount) ? nCount : nStartPos;
75 0 : nStartPos -= nDone;
76 : }
77 :
78 0 : return nStartPos;
79 : }
80 :
81 0 : sal_Int32 SAL_CALL BreakIterator_CTL::nextCharacters(const OUString& Text,
82 : sal_Int32 nStartPos, const lang::Locale& rLocale,
83 : sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone)
84 : throw(RuntimeException, std::exception)
85 : {
86 0 : sal_Int32 len = Text.getLength();
87 0 : if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
88 0 : nDone = 0;
89 0 : if (nStartPos < len) {
90 0 : makeIndex(Text, nStartPos);
91 :
92 0 : if (nextCellIndex[nStartPos] == 0) // not a CTL character
93 : return BreakIterator_Unicode::nextCharacters(Text, nStartPos, rLocale,
94 0 : nCharacterIteratorMode, nCount, nDone);
95 0 : else while (nCount > 0 && nextCellIndex[nStartPos] > 0) {
96 0 : nCount--; nDone++;
97 0 : nStartPos = nextCellIndex[nStartPos];
98 : }
99 : } else
100 0 : nStartPos = len;
101 : } else {
102 0 : nDone = (len - nStartPos > nCount) ? nCount : len - nStartPos;
103 0 : nStartPos += nDone;
104 : }
105 :
106 0 : return nStartPos;
107 : }
108 :
109 : // This method should be overwritten by derived language specific class.
110 0 : void SAL_CALL BreakIterator_CTL::makeIndex(const OUString& /*text*/, sal_Int32 /*pos*/)
111 : throw(RuntimeException)
112 : {
113 0 : throw RuntimeException();
114 : }
115 :
116 : // Make sure line is broken on cell boundary if we implement cell iterator.
117 0 : LineBreakResults SAL_CALL BreakIterator_CTL::getLineBreak(
118 : const OUString& Text, sal_Int32 nStartPos,
119 : const lang::Locale& rLocale, sal_Int32 nMinBreakPos,
120 : const LineBreakHyphenationOptions& hOptions,
121 : const LineBreakUserOptions& bOptions ) throw(RuntimeException, std::exception)
122 : {
123 : LineBreakResults lbr = BreakIterator_Unicode::getLineBreak(Text, nStartPos,
124 0 : rLocale, nMinBreakPos, hOptions, bOptions );
125 0 : if (lbr.breakIndex < Text.getLength()) {
126 0 : makeIndex(Text, lbr.breakIndex);
127 0 : lbr.breakIndex = previousCellIndex[ lbr.breakIndex ];
128 : }
129 0 : return lbr;
130 : }
131 :
132 : } } } }
133 :
134 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|