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 .
22 #include <rtl/ustring.hxx>
23 #include <sal/types.h>
25 #include "fmtautofmt.hxx"
30 #include <unordered_map>
34 class SwFormatAutoFormat
;
36 class SwChapterFieldType
;
45 struct SwTOXSortTabBase
;
46 class ToxTextGeneratorTest
;
50 class ToxLinkProcessor
;
51 class ToxTabStopTokenHandler
;
52 class ToxWhitespaceStripper
;
54 /** This class generates text for the entries of a table of x.
56 * You can control its behavior by calling @link SetTabstopPolicy() and specifying the desired behavior.
58 class ToxTextGenerator
61 ToxTextGenerator(const SwForm
& toxForm
, std::shared_ptr
<ToxTabStopTokenHandler
> tabStopHandler
);
63 virtual ~ToxTextGenerator();
65 /** Generate the text for an entry of a table of X (X is, e.g., content).
67 * This method will process the entries in @p entries, starting at @p indexOfEntryToProcess and
68 * process @p numberOfEntriesToProcess entries.
70 std::optional
<std::pair
<SwTextNode
*, SvxTabStopItem
>>
71 GenerateText(SwDoc
*doc
,
72 std::unordered_map
<OUString
, int> & rMarkURLs
,
73 const std::vector
<std::unique_ptr
<SwTOXSortTabBase
>>& entries
,
74 sal_uInt16 indexOfEntryToProcess
, sal_uInt16 numberOfEntriesToProcess
,
75 SwRootFrame
const* pLayout
);
78 const SwForm
& mToxForm
;
79 std::shared_ptr
<ToxLinkProcessor
> mLinkProcessor
;
80 std::shared_ptr
<ToxTabStopTokenHandler
> mTabStopTokenHandler
;
82 /** A handled text token.
83 * It contains the information which should be added to the target text node.
85 struct HandledTextToken
{
87 std::vector
<std::unique_ptr
<SwFormatAutoFormat
>> autoFormats
;
88 std::vector
<sal_Int32
> startPositions
;
89 std::vector
<sal_Int32
> endPositions
;
92 static void GetAttributesForNode(
93 HandledTextToken
& rResult
,
95 SwTextNode
const& rNode
,
96 ToxWhitespaceStripper
const& rStripper
,
98 SwRootFrame
const*const pLayout
);
100 /** Append text (and selected attributes) to a target node.
102 * Will take the text of @p source, and return the text and the attributes which should be added to the
103 * target text node. @see CollectAttributesForTox() for the criteria of the attributes which are taken.
105 static HandledTextToken
106 HandleTextToken(const SwTOXSortTabBase
& source
, SwAttrPool
& attrPool
,
107 SwRootFrame
const*const pLayout
);
109 /** Applies the result of a handled text token to a target node. */
111 ApplyHandledTextToken(const HandledTextToken
& htt
, SwTextNode
& targetNode
);
113 /** Handle a page number token.
115 * Will return a string of @p numberOfToxSources concatenated '@' signs, separated by commas, and
117 * (The '@' sign is the magic character C_NUM_REPL, the '~' sign is the magic character C_END_PAGE_NUM.
120 * The count of similar entries, i.e., nodes in aTOXSources of SwTOXSortTabBase gives the PagerNumber
124 ConstructPageNumberPlaceholder(size_t numberOfToxSources
);
126 /** Collect the attributes of a hint that shall be copied over to the TOX.
128 * Some text attributes are used in the TOX entries. This method defines which attributes are used.
130 * @param hint The hint from which the attributes are taken
131 * @param pool The attribute pool for the new items
133 static std::shared_ptr
<SfxItemSet
>
134 CollectAttributesForTox(const SwTextAttr
& hint
, SwAttrPool
& pool
);
136 /** This method will call GetNumStringOfFirstNode() of the first node in the provided SwTOXSortTabBase.
138 * The parameters @p bUsePrefix and @p nLevel are passed to SwTextNode::GetNumString()
141 * The method is only called if several preconditions for @p rBase are true. Check the implementation
145 GetNumStringOfFirstNode(const SwTOXSortTabBase
& rBase
, bool bUsePrefix
,
146 sal_uInt8 nLevel
, SwRootFrame
const* pLayout
, bool bAddSpace
= true);
148 /** Handle a chapter token.
151 HandleChapterToken(const SwTOXSortTabBase
& rBase
, const SwFormToken
& aToken
,
152 SwRootFrame
const* pLayout
) const;
154 /** Generate the text for a chapter token.
157 GenerateTextForChapterToken(const SwFormToken
& chapterToken
, const SwContentFrame
* contentFrame
,
158 const SwContentNode
*contentNode
, SwRootFrame
const* pLayout
) const;
160 /** Obtain a ChapterField to use for the text generation.
162 * This method is overridden in the unittests. Do not override it yourself.
164 virtual SwChapterField
165 ObtainChapterField(SwChapterFieldType
* chapterFieldType
, const SwFormToken
* chapterToken
,
166 const SwContentFrame
* contentFrame
, const SwContentNode
*contentNode
) const;
168 friend class ::ToxTextGeneratorTest
;
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */