File: ToxTabStopTokenHandler.cxx

package info (click to toggle)
libreoffice 4%3A25.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,815,320 kB
  • sloc: cpp: 4,378,739; xml: 454,679; java: 257,215; python: 80,874; ansic: 33,824; perl: 30,304; javascript: 19,722; sh: 11,717; makefile: 10,622; cs: 8,865; yacc: 8,549; objc: 2,131; lex: 1,379; asm: 1,231; awk: 996; pascal: 914; csh: 20; sed: 5
file content (168 lines) | stat: -rw-r--r-- 6,386 bytes parent folder | download | duplicates (5)
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
/* -*- 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/.
 */

#include <ToxTabStopTokenHandler.hxx>

#include <editeng/tstpitem.hxx>
#include <editeng/lrspitem.hxx>
#include <editeng/boxitem.hxx>

#include <cntfrm.hxx>
#include <fmtfsize.hxx>
#include <fmtpdsc.hxx>
#include <frmfmt.hxx>
#include <frmatr.hxx>
#include <frmtool.hxx>
#include <ndtxt.hxx>
#include <pagedesc.hxx>
#include <pagefrm.hxx>
#include <swrect.hxx>
#include <tox.hxx>

namespace sw {

DefaultToxTabStopTokenHandler::DefaultToxTabStopTokenHandler(SwNodeOffset indexOfSectionNode,
        const SwPageDesc& defaultPageDescription,
        bool tabPositionIsRelativeToParagraphIndent,
        TabStopReferencePolicy referencePolicy)
: mIndexOfSectionNode(indexOfSectionNode),
  mDefaultPageDescription(defaultPageDescription),
  mTabPositionIsRelativeToParagraphIndent(tabPositionIsRelativeToParagraphIndent),
  mTabStopReferencePolicy(referencePolicy)
{
}


ToxTabStopTokenHandler::HandledTabStopToken
DefaultToxTabStopTokenHandler::HandleTabStopToken(
        const SwFormToken& aToken, const SwTextNode& targetNode) const
{
    HandledTabStopToken result;

    if (aToken.bWithTab) { // #i21237#
        result.text = "\t";
    }

    // check whether a tab adjustment has been specified.
    if (SvxTabAdjust::End > aToken.eTabAlign) {
        SvxTextLeftMarginItem const& rTextLeftMargin(
            targetNode.SwContentNode::GetAttr(RES_MARGIN_TEXTLEFT));
        tools::Long nTabPosition = aToken.nTabStopPosition;
        if (!mTabPositionIsRelativeToParagraphIndent && rTextLeftMargin.ResolveTextLeft({}) != 0)
        {
            nTabPosition -= rTextLeftMargin.ResolveTextLeft({});
        }
        result.tabStop = SvxTabStop(nTabPosition, aToken.eTabAlign, cDfltDecimalChar, aToken.cTabFillChar);
        return result;
    }

    // note: this will be filled later by CalcEndStop()
    result.tabStop = SvxTabStop(0, SvxTabAdjust::Right, cDfltDecimalChar, aToken.cTabFillChar);
    return result;
}

auto DefaultToxTabStopTokenHandler::CalcEndStop(SwTextNode const& rNode,
        SwRootFrame const*const pLayout) const -> tools::Long
{
    tools::Long nRightMargin;
    if (CanUseLayoutRectangle(rNode, pLayout))
    {
        // in case it's in a header, any frame should do
        SwContentFrame const*const pFrame(rNode.getLayoutFrame(pLayout));
        assert(pFrame); // created in SwTOXBaseSection::Update()
        SwRectFnSet const fnRect(pFrame->GetUpper());
        SwRect rect = pFrame->getFramePrintArea();
        if (fnRect.GetWidth(rect) == 0) // typically it's newly created
        {
            if (pFrame->GetUpper()->IsSctFrame())
            {   // this is set in SwSectionFrame::Init()
                rect = pFrame->GetUpper()->GetUpper()->getFramePrintArea();
                assert(fnRect.GetWidth(rect) != 0);
            }
            else if (pFrame->GetUpper()->IsColBodyFrame())
            {
                SwFrame const*const pColFrame(pFrame->GetUpper()->GetUpper());
                assert(pColFrame->IsColumnFrame());
                rect = pColFrame->getFrameArea();
                // getFramePrintArea() is not valid yet, manually subtract...
                // (it can have a border too!)
                SwBorderAttrAccess access(SwFrame::GetCache(), pColFrame);
                SwBorderAttrs const& rAttrs(*access.Get());
                auto const nLeft(rAttrs.CalcLeft(pColFrame));
                auto const nRight(rAttrs.CalcRight(pColFrame));
                fnRect.SetWidth(rect, fnRect.GetWidth(rect) - nLeft - nRight);
                assert(fnRect.GetWidth(rect) != 0);
            }
            else assert(false);
        }
        nRightMargin = fnRect.GetWidth(rect);
    }
    else
    {
        nRightMargin = CalculatePageMarginFromPageDescription(rNode);
    }
    //#i24363# tab stops relative to indent
    if (mTabStopReferencePolicy == TABSTOPS_RELATIVE_TO_INDENT) {
        // left margin of paragraph style
        SvxFirstLineIndentItem const& rFirstLine(
            rNode.GetTextColl()->GetFirstLineIndent());
        SvxTextLeftMarginItem const& rTextLeftMargin(
            rNode.GetTextColl()->GetTextLeftMargin());

        nRightMargin -= rTextLeftMargin.ResolveLeft(rFirstLine, /*metrics*/ {});
        nRightMargin -= rFirstLine.ResolveTextFirstLineOffset(/*metrics*/ {});
    }
    return nRightMargin - 1; // subtract 1 twip to avoid equal for TabOverMargin
}

tools::Long
DefaultToxTabStopTokenHandler::CalculatePageMarginFromPageDescription(const SwTextNode& targetNode) const
{
    SwNodeOffset nPgDescNdIdx = targetNode.GetIndex() + 1;
    const SwPageDesc *pPageDesc = targetNode.FindPageDesc(&nPgDescNdIdx);
    if (!pPageDesc || nPgDescNdIdx < mIndexOfSectionNode) {
        // Use default page description, if none is found or the found one is given by a Node before the
        // table-of-content section.
        pPageDesc = &mDefaultPageDescription;
    }
    const SwFrameFormat& rPgDscFormat = pPageDesc->GetMaster();
    tools::Long result = rPgDscFormat.GetFrameSize().GetWidth()
                         - rPgDscFormat.GetLRSpace().ResolveLeft({})
                         - rPgDscFormat.GetLRSpace().ResolveRight({});
    // Also consider borders
    const SvxBoxItem& rBox = rPgDscFormat.GetBox();
    result -= rBox.CalcLineSpace(SvxBoxItemLine::LEFT) + rBox.CalcLineSpace(SvxBoxItemLine::RIGHT);
    return result;
}


/*static*/ bool
DefaultToxTabStopTokenHandler::CanUseLayoutRectangle(const SwTextNode& targetNode, const SwRootFrame *currentLayout)
{
    const SwPageDesc* pageDescription =
            targetNode.SwContentNode::GetAttr(RES_PAGEDESC).GetPageDesc();

    if (!pageDescription) {
        return true;
    }
    const SwFrame* pFrame = targetNode.getLayoutFrame(currentLayout);
    if (!pFrame) {
        return false;
    }
    pFrame = pFrame->FindPageFrame();
    if (!pFrame) {
        return false;
    }
    const SwPageFrame* pageFrame = static_cast<const SwPageFrame*>(pFrame);
    return pageDescription == pageFrame->GetPageDesc();
}

}

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