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 : #ifndef SW_TOXTEXTGENERATOR_HXX_
21 : #define SW_TOXTEXTGENERATOR_HXX_
22 :
23 : #include "rtl/ustring.hxx"
24 : #include "sal/types.h"
25 : #include "swdllapi.h"
26 :
27 : #include <memory>
28 : #include <vector>
29 :
30 : class SfxItemSet;
31 : class SwAttrPool;
32 : class SwFormatAutoFormat;
33 : class SwChapterField;
34 : class SwChapterFieldType;
35 : class SwContentFrm;
36 : class SwContentNode;
37 : class SwDoc;
38 : class SwForm;
39 : struct SwFormToken;
40 : class SwPageDesc;
41 : class SwRootFrm;
42 : class SwTextAttr;
43 : class SwTextNode;
44 : struct SwTOXSortTabBase;
45 : class SvxTabStop;
46 : class ToxTextGeneratorTest;
47 :
48 : namespace sw {
49 :
50 : class ToxLinkProcessor;
51 : class ToxTabStopTokenHandler;
52 :
53 : /** This class generates text for the entries of a table of x.
54 : *
55 : * You can control its behavior by calling @link SetTabstopPolicy() and specifying the desired behavior.
56 : */
57 : class ToxTextGenerator
58 : {
59 : public:
60 : ToxTextGenerator(const SwForm& toxForm, std::shared_ptr<ToxTabStopTokenHandler> tabStopHandler);
61 :
62 : virtual ~ToxTextGenerator();
63 :
64 : /** Generate the text for an entry of a table of X (X is, e.g., content).
65 : *
66 : * This method will process the entries in @p entries, starting at @p indexOfEntryToProcess and
67 : * process @p numberOfEntriesToProcess entries.
68 : */
69 : void
70 : GenerateText(SwDoc *doc, const std::vector<SwTOXSortTabBase*>& entries,
71 : sal_uInt16 indexOfEntryToProcess, sal_uInt16 numberOfEntriesToProcess);
72 :
73 : private:
74 : const SwForm& mToxForm;
75 : std::shared_ptr<ToxLinkProcessor> mLinkProcessor;
76 : std::shared_ptr<ToxTabStopTokenHandler> mTabStopTokenHandler;
77 :
78 : /** A handled text token.
79 : * It contains the information which should be added to the target text node.
80 : */
81 6 : struct HandledTextToken {
82 : OUString text;
83 : std::vector<SwFormatAutoFormat*> autoFormats;
84 : std::vector<sal_Int32> startPositions;
85 : std::vector<sal_Int32> endPositions;
86 : };
87 : /** Append text (and selected attributes) to a target node.
88 : *
89 : * Will take the text of @p source, and return the text and the attributes which should be added to the
90 : * target text node. @see CollectAttributesForTox() for the criteria of the attributes which are taken.
91 : */
92 : static HandledTextToken
93 : HandleTextToken(const SwTOXSortTabBase& source, SwAttrPool& attrPool);
94 :
95 : /** Applies the result of a handled text token to a target node. */
96 : static void
97 : ApplyHandledTextToken(const HandledTextToken& htt, SwTextNode& targetNode);
98 :
99 : /** Handle a page number token.
100 : *
101 : * Will return a string of @p numberOfToxSources concatenated '@' signs, separated by commas, and
102 : * finished by a '~'.
103 : * (The '@' sign is the magic character C_NUM_REPL, the '~' sign is the magic character C_END_PAGE_NUM.
104 : *
105 : * @internal
106 : * The count of similar entries, i.e., nodes in aTOXSources of SwTOXSortTabBase gives the PagerNumber
107 : * pattern.
108 : */
109 : static OUString
110 : ConstructPageNumberPlaceholder(size_t numberOfToxSources);
111 :
112 : /** Collect the attributes of a hint that shall be copied over to the TOX.
113 : *
114 : * Some text attributes are used in the TOX entries. This method defines which attributes are used.
115 : *
116 : * @param hint The hint from which the attributes are taken
117 : * @param pool The attribute pool for the new items
118 : */
119 : static std::shared_ptr<SfxItemSet>
120 : CollectAttributesForTox(const SwTextAttr& hint, SwAttrPool& pool);
121 :
122 : /** This method will call GetNumStringOfFirstNode() of the first node in the provided SwTOXSortTabBase.
123 : *
124 : * The parameters @p bUsePrefix and @p nLevel are passed to SwTextNode::GetNumString()
125 : *
126 : * @internal
127 : * The method is only called if several preconditions for @p rBase are true. Check the implementation
128 : * for details.
129 : */
130 : static OUString
131 : GetNumStringOfFirstNode(const SwTOXSortTabBase& rBase, bool bUsePrefix, sal_uInt8 nLevel);
132 :
133 : /** Handle a chapter token.
134 : */
135 : OUString
136 : HandleChapterToken(const SwTOXSortTabBase& rBase, const SwFormToken& aToken, SwDoc* pDoc) const;
137 :
138 : /** Generate the text for a chapter token.
139 : */
140 : OUString
141 : GenerateTextForChapterToken(const SwFormToken& chapterToken, const SwContentFrm* contentFrame,
142 : const SwContentNode *contentNode) const;
143 :
144 : /** Obtain a ChapterField to use for the text generation.
145 : * @internal
146 : * This method is overridden in the unittests. Do not override it yourself.
147 : */
148 : virtual SwChapterField
149 : ObtainChapterField(SwChapterFieldType* chapterFieldType, const SwFormToken* chapterToken,
150 : const SwContentFrm* contentFrame, const SwContentNode *contentNode) const;
151 :
152 : friend class ::ToxTextGeneratorTest;
153 : };
154 :
155 : }
156 :
157 : #endif /* SW_TOXTEXTGENERATOR_HXX_ */
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|