Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / txtnode / chrfmt.cxx
blobe22b6bab0421f5bb4ec822a2aee8e7ac64bf530b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
24 #include <doc.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())
51 return;
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
85 ByName::iterator it
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())
99 return *it;
100 return nullptr;
103 void SwCharFormats::DeleteAndDestroyAll(bool keepDefault)
105 if (empty())
106 return;
107 const int _offset = keepDefault ? 1 : 0;
108 for (const_iterator it = begin() + _offset; it != end(); ++it)
110 assert(!(*it)->HasName(u"Character style"));
111 delete *it;
113 if (_offset)
114 m_PosIndex.erase(begin() + _offset, end());
115 else
116 m_Array.clear();
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)
132 auto it = find(v);
133 erase(it);
134 v->SetFormatName(sNewName);
135 insert(v);
138 size_t SwCharFormats::GetPos(const SwCharFormat* p) const
140 auto it = find(p);
141 return it == end() ? SIZE_MAX : it - begin();
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */