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 .
20 #ifndef INCLUDED_SW_INC_MODELTOVIEWHELPER_HXX
21 #define INCLUDED_SW_INC_MODELTOVIEWHELPER_HXX
23 #include <rtl/ustring.hxx>
24 #include <sal/types.h>
25 #include <o3tl/typed_flags_set.hxx>
30 /** Some helpers for converting model strings to view strings.
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
39 CH_TXTATR_BREAKWORD -> SwTextNode will have field attributes associated with these
43 AAAAA BBBBB # CCCCC # DDDDD
49 | | .......... bounds of a hidden text character attribute
53 .............. a range of text defined in redline region as deleted
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
68 ExpandFields
= 0x0001,
69 ExpandFootnote
= 0x0002,
70 HideInvisible
= 0x0004,
71 HideDeletions
= 0x0008,
72 /// do not expand to content, but replace with zwsp
78 template<> struct typed_flags
<ExpandMode
> : is_typed_flags
<ExpandMode
, 0x001f> {};
81 class ModelToViewHelper
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.
89 struct ConversionMapEntry
91 ConversionMapEntry(sal_Int32 nModelPos
, sal_Int32 nViewPos
, bool bVisible
)
92 : m_nModelPos(nModelPos
)
93 , m_nViewPos(nViewPos
)
94 , m_bVisible(bVisible
)
97 sal_Int32 m_nModelPos
;
101 typedef std::vector
< ConversionMapEntry
> ConversionMap
;
102 typedef std::vector
<sal_Int32
> Positions
;
104 ConversionMap m_aMap
;
105 /// store positions of fields and footnotes for grammar checkers
106 Positions m_FieldPositions
;
107 Positions m_FootnotePositions
;
113 /** This struct defines a position in the model string.
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
125 ModelPosition() : mnPos(0), mnSubPos(0), mbIsField(false) {}
128 ModelToViewHelper(const SwTextNode
&rNode
,
129 // defaults are appropriate for spell/grammar checking
130 ExpandMode eMode
= ExpandMode::ExpandFields
| ExpandMode::ExpandFootnote
| ExpandMode::ReplaceMode
);
131 ModelToViewHelper() //pass through filter, view == model
135 /** Converts a model position into a view position
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.
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
148 sal_Int32
ConvertToViewPosition( sal_Int32 nModelPos
) const;
150 /** Converts a view position into a model position
153 nPos denotes a position in the view string which should be
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
163 ModelPosition
ConvertToModelPosition( sal_Int32 nViewPos
) const;
165 OUString
getViewText() const { return m_aRetText
; }
166 Positions
const& getFieldPositions() const { return m_FieldPositions
; }
167 Positions
const& getFootnotePositions() const { return m_FootnotePositions
;}
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */