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 
10 #pragma once
11 
12 #include <string>
13 #include <utility>
14 #include <vector>
15 #include <map>
16 
17 #include <tools/ref.hxx>
18 #include "rtfvalue.hxx"
19 
20 namespace writerfilter::rtftok
21 {
22 using RTFSprmsImplBase = std::vector<std::pair<Id, RTFValue::Pointer_t>>;
23 
24 /// The payload of RTFSprms which is only copied on write.
25 class RTFSprmsImpl : public RTFSprmsImplBase, public virtual SvRefBase
26 {
27 };
28 
29 enum class RTFOverwrite
30 {
31     YES, ///< Yes, if an existing key is found, overwrite it.
32     NO_APPEND, ///< No, always append the value to the end of the list.
33     NO_IGNORE, ///< No, if the key is already in the list, then ignore, otherwise append.
34     YES_PREPEND ///< Yes, always prepend the value to the start of the list and remove existing entries.
35 };
36 
37 /// A list of RTFSprm with a copy constructor that performs a deep copy.
38 class RTFSprms : public virtual SvRefBase
39 {
40 public:
41     using Pointer_t = tools::SvRef<RTFSprms>;
42     using Entry_t = std::pair<Id, RTFValue::Pointer_t>;
43     using Iterator_t = std::vector<Entry_t>::iterator;
44     using ReverseIterator_t = std::vector<Entry_t>::reverse_iterator;
45     RTFSprms();
46     ~RTFSprms() override;
47 
48     RTFSprms(RTFSprms const&) = default;
49     RTFSprms(RTFSprms&&) = default;
50     RTFSprms& operator=(RTFSprms const&) = default;
51     RTFSprms& operator=(RTFSprms&&) = default;
52 
53     RTFValue::Pointer_t find(Id nKeyword, bool bFirst = true, bool bForWrite = false);
54     /// Does the same as ->push_back(), except that it can overwrite or ignore existing entries.
55     void set(Id nKeyword, const RTFValue::Pointer_t& pValue,
56              RTFOverwrite eOverwrite = RTFOverwrite::YES);
57     bool erase(Id nKeyword);
58     void eraseLast(Id nKeyword);
59     /// Removes elements which are already in the reference set.
60     /// Also insert default values to override attributes of style
61     /// (yes, really; that's what Word does).
62     /// @param bImplicitPPr implicit dereference of top-level pPr SPRM
63     /// @param pDirect pointer to the root of the direct formatting SPRM tree, if any
64     RTFSprms cloneAndDeduplicate(RTFSprms& rReference, Id nStyleType, bool bImplicitPPr = false,
65                                  RTFSprms* pDirect = nullptr) const;
66     /// Inserts default values to override attributes of pAbstract.
67     void duplicateList(const RTFValue::Pointer_t& pAbstract);
68     /// Removes duplicated values based on in-list properties.
69     void deduplicateList(const std::map<int, int>& rInvalidListLevelFirstIndents);
size() const70     std::size_t size() const { return m_pSprms->size(); }
empty() const71     bool empty() const { return m_pSprms->empty(); }
back()72     Entry_t& back() { return m_pSprms->back(); }
begin()73     Iterator_t begin() { return m_pSprms->begin(); }
end()74     Iterator_t end() { return m_pSprms->end(); }
75     void clear();
76     bool equals(const RTFValue& rOther) const;
77 
78 private:
79     void ensureCopyBeforeWrite();
80     tools::SvRef<RTFSprmsImpl> m_pSprms;
81 };
82 
83 /// RTF keyword with a parameter
84 class RTFSprm : public Sprm
85 {
86 public:
87     RTFSprm(Id nKeyword, RTFValue::Pointer_t& pValue);
88     sal_uInt32 getId() const override;
89     Value::Pointer_t getValue() override;
90     writerfilter::Reference<Properties>::Pointer_t getProps() override;
91 #ifdef DBG_UTIL
92     std::string getName() const override;
93     std::string toString() const override;
94 #endif
95 private:
96     Id m_nKeyword;
97     RTFValue::Pointer_t& m_pValue;
98 };
99 } // namespace writerfilter::rtftok
100 
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
102