summaryrefslogtreecommitdiff
path: root/sw/source/core/txtnode/txatritr.cxx
blob: a09bb94344c6b698089fd509379705f7c5e172f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <txatritr.hxx>

#include <com/sun/star/i18n/ScriptType.hpp>
#include <fchrfmt.hxx>
#include <charfmt.hxx>
#include <breakit.hxx>
#include <ndtxt.hxx>
#include <txatbase.hxx>

using namespace ::com::sun::star;


SwScriptIterator::SwScriptIterator(
            const OUString& rStr, sal_Int32 nStt, bool const bFrwrd)
    : m_rText(rStr)
    , m_nChgPos(rStr.getLength())
    , nCurScript(i18n::ScriptType::WEAK)
    , bForward(bFrwrd)
{
    if( g_pBreakIt->GetBreakIter().is() )
    {
        if ( ! bFrwrd && nStt )
            --nStt;

        sal_Int32 nPos = nStt;
        nCurScript = g_pBreakIt->GetBreakIter()->getScriptType(m_rText, nPos);
        if( i18n::ScriptType::WEAK == nCurScript )
        {
            if( nPos )
            {
                nPos = g_pBreakIt->GetBreakIter()->beginOfScript(
                                                m_rText, nPos, nCurScript);
                if (nPos > 0 && nPos < m_rText.getLength())
                {
                    nStt = --nPos;
                    nCurScript =
                        g_pBreakIt->GetBreakIter()->getScriptType(m_rText,nPos);
                }
            }
        }

        m_nChgPos = (bForward)
            ?  g_pBreakIt->GetBreakIter()->endOfScript(
                    m_rText, nStt, nCurScript)
            :  g_pBreakIt->GetBreakIter()->beginOfScript(
                    m_rText, nStt, nCurScript);
    }
}

bool SwScriptIterator::Next()
{
    bool bRet = false;
    if( g_pBreakIt->GetBreakIter().is() )
    {
        if (bForward && m_nChgPos >= 0 && m_nChgPos < m_rText.getLength())
        {
            nCurScript =
                g_pBreakIt->GetBreakIter()->getScriptType(m_rText, m_nChgPos);
            m_nChgPos = g_pBreakIt->GetBreakIter()->endOfScript(
                                            m_rText, m_nChgPos, nCurScript);
            bRet = true;
        }
        else if (!bForward && m_nChgPos > 0)
        {
            --m_nChgPos;
            nCurScript =
                g_pBreakIt->GetBreakIter()->getScriptType(m_rText, m_nChgPos);
            m_nChgPos = g_pBreakIt->GetBreakIter()->beginOfScript(
                                                m_rText, m_nChgPos, nCurScript);
            bRet = true;
        }
    }
    else
        m_nChgPos = m_rText.getLength();
    return bRet;
}



SwTxtAttrIterator::SwTxtAttrIterator( const SwTxtNode& rTNd, sal_uInt16 nWhchId,
                                        sal_Int32 nStt,
                                        bool bUseGetWhichOfScript )
    : aSIter( rTNd.GetTxt(), nStt ), rTxtNd( rTNd ),
    pParaItem( 0 ), nChgPos( nStt ), nAttrPos( 0 ), nWhichId( nWhchId ),
    bIsUseGetWhichOfScript( bUseGetWhichOfScript )
{
    SearchNextChg();
}

bool SwTxtAttrIterator::Next()
{
    bool bRet = false;
    if (nChgPos < aSIter.GetText().getLength())
    {
        bRet = true;
        if( !aStack.empty() )
        {
            do {
                const SwTxtAttr* pHt = aStack.front();
                sal_uInt16 nEndPos = *pHt->End();
                if( nChgPos >= nEndPos )
                    aStack.pop_front();
                else
                    break;
            } while( !aStack.empty() );
        }

        if( !aStack.empty() )
        {
            sal_uInt16 nSavePos = nAttrPos;
            SearchNextChg();
            if( !aStack.empty() )
            {
                const SwTxtAttr* pHt = aStack.front();
                const sal_uInt16 nEndPos = *pHt->End();
                if( nChgPos >= nEndPos )
                {
                    nChgPos = nEndPos;
                    nAttrPos = nSavePos;

                    if( RES_TXTATR_CHARFMT == pHt->Which() )
                    {
                        sal_uInt16 nWId = bIsUseGetWhichOfScript ?
                                GetWhichOfScript( nWhichId,
                                                  aSIter.GetCurrScript() ) : nWhichId;
                        pCurItem = &pHt->GetCharFmt().GetCharFmt()->GetFmtAttr(nWId);
                    }
                    else
                        pCurItem = &pHt->GetAttr();

                    aStack.pop_front();
                }
            }
        }
        else
            SearchNextChg();
    }
    return bRet;
}

void SwTxtAttrIterator::AddToStack( const SwTxtAttr& rAttr )
{
    sal_uInt16 nIns = 0, nEndPos = *rAttr.End();
    for( ; nIns < aStack.size(); ++nIns )
        if( *aStack[ nIns ]->End() > nEndPos )
            break;

    aStack.insert( aStack.begin() + nIns, &rAttr );
}

void SwTxtAttrIterator::SearchNextChg()
{
    sal_uInt16 nWh = 0;
    if( nChgPos == aSIter.GetScriptChgPos() )
    {
        aSIter.Next();
        pParaItem = 0;
        nAttrPos = 0;       // must be restart at the beginning, because
                            // some attributes can start before or inside
                            // the current scripttype!
        aStack.clear();
    }
    if( !pParaItem )
    {
        nWh = bIsUseGetWhichOfScript ?
                GetWhichOfScript( nWhichId,
                                  aSIter.GetCurrScript() ) : nWhichId;
        pParaItem = &rTxtNd.GetSwAttrSet().Get( nWh );
    }

    sal_Int32 nStt = nChgPos;
    nChgPos = aSIter.GetScriptChgPos();
    pCurItem = pParaItem;

    const SwpHints* pHts = rTxtNd.GetpSwpHints();
    if( pHts )
    {
        if( !nWh )
        {
            nWh =  bIsUseGetWhichOfScript ?
                        GetWhichOfScript( nWhichId,
                                          aSIter.GetCurrScript() ) : nWhichId;
        }

        const SfxPoolItem* pItem = 0;
        for( ; nAttrPos < pHts->Count(); ++nAttrPos )
        {
            const SwTxtAttr* pHt = (*pHts)[ nAttrPos ];
            const sal_Int32* pEnd = pHt->End();
            const sal_Int32 nHtStt = *pHt->GetStart();
            if( nHtStt < nStt && ( !pEnd || *pEnd <= nStt ))
                continue;

            if( nHtStt >= nChgPos )
                break;

            pItem = CharFmt::GetItem( *pHt, nWh );
            if ( pItem )
            {
                if( nHtStt > nStt )
                {
                    if( nChgPos > nHtStt )
                        nChgPos = nHtStt;
                    break;
                }
                AddToStack( *pHt );
                pCurItem = pItem;
                if( *pEnd < nChgPos )
                    nChgPos = *pEnd;
            }
        }
    }
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */