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 #include <libxml/xmlwriter.h>
22 #include <charfmt.hxx>
23 #include <charformats.hxx>
26 void SwCharFormat::dumpAsXml(xmlTextWriterPtr pWriter
) const
28 (void)xmlTextWriterStartElement(pWriter
, BAD_CAST("SwCharFormat"));
29 (void)xmlTextWriterWriteFormatAttribute(pWriter
, BAD_CAST("ptr"), "%p", this);
30 (void)xmlTextWriterWriteAttribute(pWriter
, BAD_CAST("name"),
31 BAD_CAST(GetName().toUtf8().getStr()));
33 if (mpLinkedParaFormat
)
35 (void)xmlTextWriterWriteAttribute(
36 pWriter
, BAD_CAST("linked"), BAD_CAST(mpLinkedParaFormat
->GetName().toUtf8().getStr()));
39 GetAttrSet().dumpAsXml(pWriter
);
40 (void)xmlTextWriterEndElement(pWriter
);
43 void SwCharFormat::SetLinkedParaFormat(SwTextFormatColl
* pLink
) { mpLinkedParaFormat
= pLink
; }
45 const SwTextFormatColl
* SwCharFormat::GetLinkedParaFormat() const { return mpLinkedParaFormat
; }
47 SwCharFormat::~SwCharFormat()
49 if (GetDoc()->IsInDtor())
54 for (const auto& pTextFormat
: *GetDoc()->GetTextFormatColls())
56 if (pTextFormat
->GetLinkedCharFormat() == this)
58 pTextFormat
->SetLinkedCharFormat(nullptr);
63 void SwCharFormats::dumpAsXml(xmlTextWriterPtr pWriter
) const
65 (void)xmlTextWriterStartElement(pWriter
, BAD_CAST("SwCharFormats"));
66 for (size_t i
= 0; i
< size(); ++i
)
67 GetFormat(i
)->dumpAsXml(pWriter
);
68 (void)xmlTextWriterEndElement(pWriter
);
71 SwCharFormats::SwCharFormats()
72 : m_PosIndex(m_Array
.get
<0>())
73 , m_NameIndex(m_Array
.get
<1>())
77 SwCharFormats::~SwCharFormats()
79 // default char format is owned by SwDoc
80 DeleteAndDestroyAll(true);
83 SwCharFormats::const_iterator
SwCharFormats::find(const SwCharFormat
* x
) const
86 = m_NameIndex
.find(std::make_tuple(x
->GetName(), const_cast<SwCharFormat
*>(x
)));
87 return m_Array
.project
<0>(it
);
90 SwCharFormats::ByName::const_iterator
SwCharFormats::findByName(const OUString
& name
) const
92 return m_NameIndex
.find(std::make_tuple(name
));
95 SwCharFormat
* SwCharFormats::FindFormatByName(const OUString
& rName
) const
97 auto it
= findByName(rName
);
98 if (it
!= m_NameIndex
.end())
103 void SwCharFormats::DeleteAndDestroyAll(bool keepDefault
)
107 const int _offset
= keepDefault
? 1 : 0;
108 for (const_iterator it
= begin() + _offset
; it
!= end(); ++it
)
110 assert(!(*it
)->HasName(u
"Character style"));
114 m_PosIndex
.erase(begin() + _offset
, end());
119 void SwCharFormats::insert(SwCharFormat
* x
)
121 assert(!ContainsFormat(x
));
122 m_PosIndex
.push_back(x
);
125 void SwCharFormats::erase(const_iterator
const& position
) { m_PosIndex
.erase(position
); }
127 bool SwCharFormats::ContainsFormat(const SwCharFormat
* x
) const { return find(x
) != end(); }
129 /** Need to call this when the format name changes */
130 void SwCharFormats::SetFormatNameAndReindex(SwCharFormat
* v
, const OUString
& sNewName
)
134 v
->SetFormatName(sNewName
);
138 size_t SwCharFormats::GetPos(const SwCharFormat
* p
) const
141 return it
== end() ? SIZE_MAX
: it
- begin();
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */