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 <pam.hxx>
21 : #include <frminf.hxx>
22 : #include <itrtxt.hxx>
23 :
24 0 : sal_Int32 SwTxtMargin::GetTxtStart() const
25 : {
26 0 : const OUString &rTxt = GetInfo().GetTxt();
27 0 : const sal_Int32 nEnd = nStart + pCurr->GetLen();
28 :
29 0 : for( sal_Int32 i = nStart; i < nEnd; ++i )
30 : {
31 0 : const sal_Unicode aChar = rTxt[i];
32 0 : if( CH_TAB != aChar && ' ' != aChar )
33 0 : return i;
34 : }
35 0 : return nEnd;
36 : }
37 :
38 0 : sal_Int32 SwTxtMargin::GetTxtEnd() const
39 : {
40 0 : const OUString &rTxt = GetInfo().GetTxt();
41 0 : const sal_Int32 nEnd = nStart + pCurr->GetLen();
42 0 : for( sal_Int32 i = nEnd - 1; i >= nStart; --i )
43 : {
44 0 : const sal_Unicode aChar = rTxt[i];
45 0 : if( CH_TAB != aChar && CH_BREAK != aChar && ' ' != aChar )
46 0 : return i + 1;
47 : }
48 0 : return nStart;
49 : }
50 :
51 : // Does the paragraph fit into one line?
52 0 : bool SwTxtFrmInfo::IsOneLine() const
53 : {
54 0 : const SwLineLayout *pLay = pFrm->GetPara();
55 0 : if( !pLay )
56 0 : return false;
57 :
58 : // For follows false of course
59 0 : if( pFrm->GetFollow() )
60 0 : return false;
61 :
62 0 : pLay = pLay->GetNext();
63 0 : while( pLay )
64 : {
65 0 : if( pLay->GetLen() )
66 0 : return false;
67 0 : pLay = pLay->GetNext();
68 : }
69 0 : return true;
70 : }
71 :
72 : // Is the line filled for X percent?
73 0 : bool SwTxtFrmInfo::IsFilled( const sal_uInt8 nPercent ) const
74 : {
75 0 : const SwLineLayout *pLay = pFrm->GetPara();
76 0 : if( !pLay )
77 0 : return false;
78 :
79 0 : long nWidth = pFrm->Prt().Width();
80 0 : nWidth *= nPercent;
81 0 : nWidth /= 100;
82 0 : return sal_uInt16(nWidth) <= pLay->Width();
83 : }
84 :
85 : // Where does the text start (without whitespace)? (document global)
86 0 : SwTwips SwTxtFrmInfo::GetLineStart( const SwTxtCursor &rLine ) const
87 : {
88 0 : const sal_Int32 nTxtStart = rLine.GetTxtStart();
89 0 : if( rLine.GetStart() == nTxtStart )
90 0 : return rLine.GetLineStart();
91 :
92 0 : SwRect aRect;
93 0 : if( ((SwTxtCursor&)rLine).GetCharRect( &aRect, nTxtStart ) )
94 0 : return aRect.Left();
95 :
96 0 : return rLine.GetLineStart();
97 : }
98 :
99 : // Where does the text start (without whitespace)? (relative in the Frame)
100 0 : SwTwips SwTxtFrmInfo::GetLineStart() const
101 : {
102 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
103 0 : SwTxtCursor aLine( (SwTxtFrm*)pFrm, &aInf );
104 0 : return GetLineStart( aLine ) - pFrm->Frm().Left() - pFrm->Prt().Left();
105 : }
106 :
107 : // Calculates the character's position and returns the middle position
108 0 : SwTwips SwTxtFrmInfo::GetCharPos( sal_Int32 nChar, bool bCenter ) const
109 : {
110 0 : SWRECTFN( pFrm )
111 0 : SwFrmSwapper aSwapper( pFrm, true );
112 :
113 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
114 0 : SwTxtCursor aLine( (SwTxtFrm*)pFrm, &aInf );
115 :
116 : SwTwips nStt, nNext;
117 0 : SwRect aRect;
118 0 : if( ((SwTxtCursor&)aLine).GetCharRect( &aRect, nChar ) )
119 : {
120 0 : if ( bVert )
121 0 : pFrm->SwitchHorizontalToVertical( aRect );
122 :
123 0 : nStt = (aRect.*fnRect->fnGetLeft)();
124 : }
125 : else
126 0 : nStt = aLine.GetLineStart();
127 :
128 0 : if( !bCenter )
129 0 : return nStt - (pFrm->Frm().*fnRect->fnGetLeft)();
130 :
131 0 : if( ((SwTxtCursor&)aLine).GetCharRect( &aRect, nChar+1 ) )
132 : {
133 0 : if ( bVert )
134 0 : pFrm->SwitchHorizontalToVertical( aRect );
135 :
136 0 : nNext = (aRect.*fnRect->fnGetLeft)();
137 : }
138 : else
139 0 : nNext = aLine.GetLineStart();
140 :
141 0 : return (( nNext + nStt ) / 2 ) - (pFrm->Frm().*fnRect->fnGetLeft)();
142 : }
143 :
144 0 : SwPaM *AddPam( SwPaM *pPam, const SwTxtFrm* pTxtFrm,
145 : const sal_Int32 nPos, const sal_Int32 nLen )
146 : {
147 0 : if( nLen )
148 : {
149 : // It could be the first
150 0 : if( pPam->HasMark() )
151 : {
152 : // If the new position is right after the current one, then
153 : // simply extend the Pam
154 0 : if( nPos == pPam->GetPoint()->nContent.GetIndex() )
155 : {
156 0 : pPam->GetPoint()->nContent += nLen;
157 0 : return pPam;
158 : }
159 0 : pPam = new SwPaM( *pPam );
160 : }
161 :
162 0 : SwIndex &rContent = pPam->GetPoint()->nContent;
163 0 : rContent.Assign( (SwTxtNode*)pTxtFrm->GetTxtNode(), nPos );
164 0 : pPam->SetMark();
165 0 : rContent += nLen;
166 : }
167 0 : return pPam;
168 : }
169 :
170 : // Accumulates the whitespace at line start and end in the Pam
171 0 : void SwTxtFrmInfo::GetSpaces( SwPaM &rPam, bool bWithLineBreak ) const
172 : {
173 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
174 0 : SwTxtMargin aLine( (SwTxtFrm*)pFrm, &aInf );
175 0 : SwPaM *pPam = &rPam;
176 0 : bool bFirstLine = true;
177 0 : do {
178 :
179 0 : if( aLine.GetCurr()->GetLen() )
180 : {
181 0 : sal_Int32 nPos = aLine.GetTxtStart();
182 : // Do NOT include the blanks/tabs from the first line
183 : // in the selection
184 0 : if( !bFirstLine && nPos > aLine.GetStart() )
185 : pPam = AddPam( pPam, pFrm, aLine.GetStart(),
186 0 : nPos - aLine.GetStart() );
187 :
188 : // Do NOT include the blanks/tabs from the last line
189 : // in the selection
190 0 : if( aLine.GetNext() )
191 : {
192 0 : nPos = aLine.GetTxtEnd();
193 :
194 0 : if( nPos < aLine.GetEnd() )
195 : {
196 0 : sal_uInt16 nOff = !bWithLineBreak && CH_BREAK ==
197 0 : aLine.GetInfo().GetChar( aLine.GetEnd() - 1 )
198 0 : ? 1 : 0;
199 0 : pPam = AddPam( pPam, pFrm, nPos, aLine.GetEnd() - nPos - nOff );
200 : }
201 : }
202 : }
203 0 : bFirstLine = false;
204 : }
205 0 : while( aLine.Next() );
206 0 : }
207 :
208 : // Is there a bullet/symbol etc. at the text position?
209 : // Fonts: CharSet, SYMBOL und DONTKNOW
210 0 : bool SwTxtFrmInfo::IsBullet( sal_Int32 nTxtStart ) const
211 : {
212 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
213 0 : SwTxtMargin aLine( (SwTxtFrm*)pFrm, &aInf );
214 0 : aInf.SetIdx( nTxtStart );
215 0 : return aLine.IsSymbol( nTxtStart );
216 : }
217 :
218 : // Get first line indent
219 : // The precondition for a positive or negative first line indent:
220 : // All lines (except for the first one) have the same left margin.
221 : // We do not want to be so picky and work with a tolerance of TOLERANCE twips.
222 0 : SwTwips SwTxtFrmInfo::GetFirstIndent() const
223 : {
224 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
225 0 : SwTxtCursor aLine( (SwTxtFrm*)pFrm, &aInf );
226 0 : const SwTwips nFirst = GetLineStart( aLine );
227 0 : const SwTwips TOLERANCE = 20;
228 :
229 0 : if( !aLine.Next() )
230 0 : return 0;
231 :
232 0 : SwTwips nLeft = GetLineStart( aLine );
233 0 : while( aLine.Next() )
234 : {
235 0 : if( aLine.GetCurr()->GetLen() )
236 : {
237 0 : const SwTwips nCurrLeft = GetLineStart( aLine );
238 0 : if( nLeft + TOLERANCE < nCurrLeft ||
239 0 : nLeft - TOLERANCE > nCurrLeft )
240 0 : return 0;
241 : }
242 : }
243 :
244 : // At first we only return +1, -1 and 0
245 0 : if( nLeft == nFirst )
246 0 : return 0;
247 :
248 0 : if( nLeft > nFirst )
249 0 : return -1;
250 :
251 0 : return 1;
252 : }
253 :
254 0 : sal_Int32 SwTxtFrmInfo::GetBigIndent( sal_Int32& rFndPos,
255 : const SwTxtFrm *pNextFrm ) const
256 : {
257 0 : SwTxtSizeInfo aInf( (SwTxtFrm*)pFrm );
258 0 : SwTxtCursor aLine( (SwTxtFrm*)pFrm, &aInf );
259 0 : SwTwips nNextIndent = 0;
260 :
261 0 : if( pNextFrm )
262 : {
263 : // I'm a single line
264 0 : SwTxtSizeInfo aNxtInf( (SwTxtFrm*)pNextFrm );
265 0 : SwTxtCursor aNxtLine( (SwTxtFrm*)pNextFrm, &aNxtInf );
266 0 : nNextIndent = GetLineStart( aNxtLine );
267 : }
268 : else
269 : {
270 : // I'm multi-line
271 0 : if( aLine.Next() )
272 : {
273 0 : nNextIndent = GetLineStart( aLine );
274 0 : aLine.Prev();
275 : }
276 : }
277 :
278 0 : if( nNextIndent <= GetLineStart( aLine ) )
279 0 : return 0;
280 :
281 0 : const Point aPoint( nNextIndent, aLine.Y() );
282 0 : rFndPos = aLine.GetCrsrOfst( 0, aPoint, false );
283 0 : if( 1 >= rFndPos )
284 0 : return 0;
285 :
286 : // Is on front of a non-space
287 0 : const OUString& rTxt = aInf.GetTxt();
288 0 : sal_Unicode aChar = rTxt[rFndPos];
289 0 : if( CH_TAB == aChar || CH_BREAK == aChar || ' ' == aChar ||
290 0 : (( CH_TXTATR_BREAKWORD == aChar || CH_TXTATR_INWORD == aChar ) &&
291 0 : aInf.HasHint( rFndPos ) ) )
292 0 : return 0;
293 :
294 : // and after a space
295 0 : aChar = rTxt[rFndPos - 1];
296 0 : if( CH_TAB != aChar && CH_BREAK != aChar &&
297 0 : ( ( CH_TXTATR_BREAKWORD != aChar && CH_TXTATR_INWORD != aChar ) ||
298 0 : !aInf.HasHint( rFndPos - 1 ) ) &&
299 : // More than two Blanks!
300 0 : ( ' ' != aChar || ' ' != rTxt[rFndPos - 2] ) )
301 0 : return 0;
302 :
303 0 : SwRect aRect;
304 0 : return aLine.GetCharRect( &aRect, rFndPos )
305 0 : ? static_cast<sal_Int32>(aRect.Left() - pFrm->Frm().Left() - pFrm->Prt().Left())
306 0 : : 0;
307 270 : }
308 :
309 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|