1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ImplLayoutRuns.hxx>
23 void ImplLayoutRuns::AddPos( int nCharPos
, bool bRTL
)
25 // check if charpos could extend current run
26 int nIndex
= maRuns
.size();
29 int nRunPos0
= maRuns
[ nIndex
-2 ];
30 int nRunPos1
= maRuns
[ nIndex
-1 ];
31 if( ((nCharPos
+ int(bRTL
)) == nRunPos1
) && ((nRunPos0
> nRunPos1
) == bRTL
) )
33 // extend current run by new charpos
34 maRuns
[ nIndex
-1 ] = nCharPos
+ int(!bRTL
);
37 // ignore new charpos when it is in current run
38 if( (nRunPos0
<= nCharPos
) && (nCharPos
< nRunPos1
) )
40 if( (nRunPos1
<= nCharPos
) && (nCharPos
< nRunPos0
) )
44 // else append a new run consisting of the new charpos
45 maRuns
.push_back( nCharPos
+ (bRTL
? 1 : 0) );
46 maRuns
.push_back( nCharPos
+ (bRTL
? 0 : 1) );
49 void ImplLayoutRuns::AddRun( int nCharPos0
, int nCharPos1
, bool bRTL
)
51 if( nCharPos0
== nCharPos1
)
55 if( bRTL
== (nCharPos0
< nCharPos1
) )
56 std::swap( nCharPos0
, nCharPos1
);
58 if (maRuns
.size() >= 2 && nCharPos0
== maRuns
[maRuns
.size() - 2] && nCharPos1
== maRuns
[maRuns
.size() - 1])
60 //this run is the same as the last
65 maRuns
.push_back( nCharPos0
);
66 maRuns
.push_back( nCharPos1
);
69 bool ImplLayoutRuns::PosIsInRun( int nCharPos
) const
71 if( mnRunIndex
>= static_cast<int>(maRuns
.size()) )
74 int nMinCharPos
= maRuns
[ mnRunIndex
+0 ];
75 int nEndCharPos
= maRuns
[ mnRunIndex
+1 ];
76 if( nMinCharPos
> nEndCharPos
) // reversed in RTL case
77 std::swap( nMinCharPos
, nEndCharPos
);
79 if( nCharPos
< nMinCharPos
)
81 if( nCharPos
>= nEndCharPos
)
86 bool ImplLayoutRuns::PosIsInAnyRun( int nCharPos
) const
89 int nRunIndex
= mnRunIndex
;
91 ImplLayoutRuns
*pThis
= const_cast<ImplLayoutRuns
*>(this);
95 for (size_t i
= 0; i
< maRuns
.size(); i
+=2)
97 bRet
= PosIsInRun( nCharPos
);
103 pThis
->mnRunIndex
= nRunIndex
;
107 bool ImplLayoutRuns::GetNextPos( int* nCharPos
, bool* bRightToLeft
)
109 // negative nCharPos => reset to first run
113 // return false when all runs completed
114 if( mnRunIndex
>= static_cast<int>(maRuns
.size()) )
117 int nRunPos0
= maRuns
[ mnRunIndex
+0 ];
118 int nRunPos1
= maRuns
[ mnRunIndex
+1 ];
119 *bRightToLeft
= (nRunPos0
> nRunPos1
);
123 // get first valid nCharPos in run
124 *nCharPos
= nRunPos0
;
128 // advance to next nCharPos for LTR case
132 // advance to next run if current run is completed
133 if( *nCharPos
== nRunPos1
)
135 if( (mnRunIndex
+= 2) >= static_cast<int>(maRuns
.size()) )
137 nRunPos0
= maRuns
[ mnRunIndex
+0 ];
138 nRunPos1
= maRuns
[ mnRunIndex
+1 ];
139 *bRightToLeft
= (nRunPos0
> nRunPos1
);
140 *nCharPos
= nRunPos0
;
144 // advance to next nCharPos for RTL case
151 bool ImplLayoutRuns::GetRun( int* nMinRunPos
, int* nEndRunPos
, bool* bRightToLeft
) const
153 if( mnRunIndex
>= static_cast<int>(maRuns
.size()) )
156 int nRunPos0
= maRuns
[ mnRunIndex
+0 ];
157 int nRunPos1
= maRuns
[ mnRunIndex
+1 ];
158 *bRightToLeft
= (nRunPos1
< nRunPos0
) ;
161 *nMinRunPos
= nRunPos0
;
162 *nEndRunPos
= nRunPos1
;
166 *nMinRunPos
= nRunPos1
;
167 *nEndRunPos
= nRunPos0
;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */