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 INCLUDED_SW_INC_MODELTOVIEWHELPER_HXX
21 : #define INCLUDED_SW_INC_MODELTOVIEWHELPER_HXX
22 :
23 : #include <rtl/ustring.hxx>
24 : #include <sal/types.h>
25 : #include <o3tl/typed_flags_set.hxx>
26 : #include <vector>
27 :
28 : class SwTextNode;
29 :
30 : /** Some helpers for converting model strings to view strings.
31 :
32 : A paragraph string does not have its fields expanded, i.e., they are
33 : represented by a special character inside the string with an additional
34 : attribute assigned to it. For some tasks (e.g., SmartTags) it is required
35 : to expand the fields to get the string as it appears in the view. Two
36 : helper functions are provided to convert model positions to view positions
37 : and vice versa.
38 :
39 : CH_TXTATR_BREAKWORD -> SwTextNode will have field attributes associated with these
40 : . .
41 : . .
42 : . .
43 : AAAAA BBBBB # CCCCC # DDDDD
44 : | | | |
45 : | | | |
46 : | ---------
47 : | | .
48 : | | .
49 : | | .......... bounds of a hidden text character attribute
50 : ------
51 : .
52 : .
53 : .............. a range of text defined in redline region as deleted
54 :
55 : 0000: pass through gives: AAAAA BBBBB # CCCCC # DDDDD
56 : 0001: only expanding fields gives: AAAAA BBBBB foo CCCCC foo DDDDD
57 : 0010: only hiding hiddens gives: AAAAA CCCCC # DDDDD
58 : 0100: only hiding redlines gives: AAAABB # CCCCC # DDDDD
59 : 0011: expanding fields + hiding hiddens gives: AAAAA CCCC foo DDDDD
60 : 0101: expanding fields + hiding redlines gives: AAAA B foo CCCCC foo DDDDD
61 : 0110: hiding hiddens + hiding redlines gives: AAAACCCC # DDDDD
62 : 0111: expanding fields + hiding hiddens + hiding redlines gives: AAAABB foo CCCCC foo DDDDD
63 : */
64 :
65 : enum class ExpandMode
66 : {
67 : PassThrough = 0x0000,
68 : ExpandFields = 0x0001,
69 : ExpandFootnote = 0x0002,
70 : HideInvisible = 0x0004,
71 : HideDeletions = 0x0008,
72 : /// do not expand to content, but replace with zwsp
73 : ReplaceMode = 0x0010,
74 : };
75 :
76 : namespace o3tl
77 : {
78 : template<> struct typed_flags<ExpandMode> : is_typed_flags<ExpandMode, 0x001f> {};
79 : }
80 :
81 145569516 : class ModelToViewHelper
82 : {
83 : /** For each expanded/hidden portion in the model string, there is an entry in
84 : the conversion map. The first value of the ConversionMapEntry points to
85 : the start position in the model string, the second value points to the
86 : associated start position in the view string. The last entry in the
87 : conversion map denotes the lengths of the model resp. view string.
88 : */
89 : struct ConversionMapEntry
90 : {
91 11720 : ConversionMapEntry(sal_Int32 nModelPos, sal_Int32 nViewPos, bool bVisible)
92 : : m_nModelPos(nModelPos)
93 : , m_nViewPos(nViewPos)
94 11720 : , m_bVisible(bVisible)
95 : {
96 11720 : }
97 : sal_Int32 m_nModelPos;
98 : sal_Int32 m_nViewPos;
99 : bool m_bVisible;
100 : };
101 : typedef std::vector< ConversionMapEntry > ConversionMap;
102 : typedef std::vector<sal_Int32> Positions;
103 :
104 : ConversionMap m_aMap;
105 : /// store positions of fields and footnotes for grammar checkers
106 : Positions m_FieldPositions;
107 : Positions m_FootnotePositions;
108 :
109 : OUString m_aRetText;
110 :
111 : public:
112 :
113 : /** This struct defines a position in the model string.
114 :
115 : The 'main' position is given by mnPos. If there's a field located at
116 : this position, mbIsField is set and mnSubPos denotes the position inside
117 : that field.
118 : */
119 : struct ModelPosition
120 : {
121 : sal_Int32 mnPos;
122 : sal_Int32 mnSubPos;
123 : bool mbIsField;
124 :
125 139856909 : ModelPosition() : mnPos(0), mnSubPos(0), mbIsField(false) {}
126 : };
127 :
128 : ModelToViewHelper(const SwTextNode &rNode,
129 : // defaults are appropriate for spell/grammar checking
130 0 : ExpandMode eMode = ExpandMode::ExpandFields | ExpandMode::ExpandFootnote | ExpandMode::ReplaceMode);
131 48516815 : ModelToViewHelper() //pass through filter, view == model
132 48516815 : {
133 48516815 : }
134 :
135 : /** Converts a model position into a view position
136 :
137 : @param nPos
138 : nPos denotes a position in the model string which should be
139 : converted. Note that converting model positions inside fields is
140 : not supported, therefore nPos is not of type ModelPosition.
141 :
142 : @return
143 : the position of nPos in the view string. In case the conversion
144 : could not be performed (e.g., because there is not ConversionMap or
145 : nPos is behind the last entry in the conversion map) nPos will
146 : be returned.
147 : */
148 : sal_Int32 ConvertToViewPosition( sal_Int32 nModelPos ) const;
149 :
150 : /** Converts a view position into a model position
151 :
152 : @param nPos
153 : nPos denotes a position in the view string which should be
154 : converted.
155 :
156 : @return
157 : the position of nPos in the model string. In case the conversion
158 : could not be performed (e.g., because there is not ConversionMap or
159 : nPos is behind the last entry in the conversion map) a model
160 : model position with mnPos = nPos and mnIsField = false will be
161 : returned.
162 : */
163 : ModelPosition ConvertToModelPosition( sal_Int32 nViewPos ) const;
164 :
165 9135 : OUString getViewText() const { return m_aRetText; }
166 7 : Positions const& getFieldPositions() const { return m_FieldPositions; }
167 12 : Positions const& getFootnotePositions() const { return m_FootnotePositions;}
168 : };
169 :
170 : #endif
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|