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