Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/table/swtablerep.cxx
Line
Count
Source
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 <tabcol.hxx>
21
22
#include <swtablerep.hxx>
23
#include <memory>
24
25
SwTableRep::SwTableRep( const SwTabCols& rTabCol )
26
    :
27
0
    m_nTableWidth(0),
28
0
    m_nSpace(0),
29
0
    m_nLeftSpace(0),
30
0
    m_nRightSpace(0),
31
0
    m_nAlign(0),
32
0
    m_nWidthPercent(0),
33
0
    m_bLineSelected(false),
34
0
    m_bWidthChanged(false),
35
0
    m_bColsChanged(false)
36
0
{
37
0
    m_nAllCols = m_nColCount = rTabCol.Count();
38
0
    m_aTColumns.resize(m_nColCount + 1);
39
0
    SwTwips nStart = 0,
40
0
            nEnd;
41
0
    for( sal_uInt16 i = 0; i < m_nAllCols; ++i )
42
0
    {
43
0
        nEnd  = rTabCol[ i ] - rTabCol.GetLeft();
44
0
        m_aTColumns[ i ].nWidth = nEnd - nStart;
45
0
        m_aTColumns[ i ].bVisible = !rTabCol.IsHidden(i);
46
0
        if(!m_aTColumns[ i ].bVisible)
47
0
            m_nColCount --;
48
0
        nStart = nEnd;
49
0
    }
50
0
    m_aTColumns[ m_nAllCols ].nWidth = rTabCol.GetRight() - rTabCol.GetLeft() - nStart;
51
0
    m_aTColumns[ m_nAllCols ].bVisible = true;
52
0
    m_nColCount++;
53
0
    m_nAllCols++;
54
0
}
55
56
SwTableRep::~SwTableRep()
57
0
{
58
0
}
59
60
bool SwTableRep::FillTabCols( SwTabCols& rTabCols ) const
61
0
{
62
0
    tools::Long nOldLeft = rTabCols.GetLeft(),
63
0
         nOldRight = rTabCols.GetRight();
64
65
0
    bool bSingleLine = false;
66
67
0
    for ( size_t i = 0; i < rTabCols.Count(); ++i )
68
0
        if(!m_aTColumns[i].bVisible)
69
0
        {
70
0
            bSingleLine = true;
71
0
            break;
72
0
        }
73
74
0
    SwTwips nPos = 0;
75
0
    const SwTwips nLeft = GetLeftSpace();
76
0
    rTabCols.SetLeft(nLeft);
77
0
    if(bSingleLine)
78
0
    {
79
        // The invisible separators are taken from the old TabCols,
80
        // the visible coming from pTColumns.
81
0
        std::unique_ptr<TColumn[]> pOldTColumns(new TColumn[m_nAllCols + 1]);
82
0
        SwTwips nStart = 0;
83
0
        for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
84
0
        {
85
0
            const SwTwips nEnd = rTabCols[i] - rTabCols.GetLeft();
86
0
            pOldTColumns[i].nWidth = nEnd - nStart;
87
0
            pOldTColumns[i].bVisible = !rTabCols.IsHidden(i);
88
0
            nStart = nEnd;
89
0
        }
90
0
        pOldTColumns[m_nAllCols - 1].nWidth = rTabCols.GetRight() - rTabCols.GetLeft() - nStart;
91
0
        pOldTColumns[m_nAllCols - 1].bVisible = true;
92
93
0
        sal_uInt16 nOldPos = 0;
94
0
        sal_uInt16 nNewPos = 0;
95
0
        SwTwips nOld = 0;
96
0
        SwTwips nNew = 0;
97
0
        bool bOld = false;
98
0
        bool bFirst = true;
99
100
0
        for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
101
0
        {
102
0
            while((bFirst || bOld ) && nOldPos < m_nAllCols )
103
0
            {
104
0
                nOld += pOldTColumns[nOldPos].nWidth;
105
0
                nOldPos++;
106
0
                if(!pOldTColumns[nOldPos - 1].bVisible)
107
0
                    break;
108
0
            }
109
0
            while((bFirst || !bOld ) && nNewPos < m_nAllCols )
110
0
            {
111
0
                nNew += m_aTColumns[nNewPos].nWidth;
112
0
                nNewPos++;
113
0
                if(pOldTColumns[nNewPos - 1].bVisible)
114
0
                    break;
115
0
            }
116
0
            bFirst = false;
117
            // They have to be inserted sorted.
118
0
            bOld = nOld < nNew;
119
0
            nPos = bOld ? nOld : nNew;
120
0
            rTabCols[i] = nPos + nLeft;
121
0
            rTabCols.SetHidden( i, bOld );
122
0
        }
123
0
        rTabCols.SetRight(nLeft + m_nTableWidth);
124
0
    }
125
0
    else
126
0
    {
127
0
        for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
128
0
        {
129
0
            nPos += m_aTColumns[i].nWidth;
130
0
            rTabCols[i] = nPos + rTabCols.GetLeft();
131
0
            rTabCols.SetHidden( i, !m_aTColumns[i].bVisible );
132
0
            rTabCols.SetRight(nLeft + m_aTColumns[m_nAllCols - 1].nWidth + nPos);
133
0
        }
134
0
    }
135
136
    // intercept rounding errors
137
0
    if(std::abs(nOldLeft - rTabCols.GetLeft()) < 3)
138
0
        rTabCols.SetLeft(nOldLeft);
139
140
0
    if(std::abs(nOldRight - rTabCols.GetRight()) < 3)
141
0
        rTabCols.SetRight(nOldRight);
142
143
0
    if(GetRightSpace() >= 0 &&
144
0
            rTabCols.GetRight() > rTabCols.GetRightMax())
145
0
        rTabCols.SetRight(rTabCols.GetRightMax());
146
0
    return bSingleLine;
147
0
}
148
149
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */