Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / starmath / source / symbol.cxx
blobd989595cc14e8b91afd2a86cbff353a0838e1afe
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 <vector>
22 #include <symbol.hxx>
23 #include <utility.hxx>
24 #include "cfgitem.hxx"
25 #include <smmod.hxx>
28 SmSym::SmSym() :
29 m_aName(OUString("unknown")),
30 m_aSetName(OUString("unknown")),
31 m_cChar('\0'),
32 m_bPredefined(false)
34 m_aExportName = m_aName;
35 m_aFace.SetTransparent(true);
36 m_aFace.SetAlignment(ALIGN_BASELINE);
40 SmSym::SmSym(const SmSym& rSymbol)
42 *this = rSymbol;
46 SmSym::SmSym(const OUString& rName, const vcl::Font& rFont, sal_UCS4 cChar,
47 const OUString& rSet, bool bIsPredefined)
49 m_aName = m_aExportName = rName;
51 m_aFace = rFont;
52 m_aFace.SetTransparent(true);
53 m_aFace.SetAlignment(ALIGN_BASELINE);
55 m_cChar = cChar;
56 m_aSetName = rSet;
57 m_bPredefined = bIsPredefined;
61 SmSym& SmSym::operator = (const SmSym& rSymbol)
63 m_aName = rSymbol.m_aName;
64 m_aExportName = rSymbol.m_aExportName;
65 m_cChar = rSymbol.m_cChar;
66 m_aFace = rSymbol.m_aFace;
67 m_aSetName = rSymbol.m_aSetName;
68 m_bPredefined = rSymbol.m_bPredefined;
70 SmSymbolManager * pSymSetManager = &SM_MOD()->GetSymbolManager();
71 if (pSymSetManager)
72 pSymSetManager->SetModified(true);
74 return *this;
78 bool SmSym::IsEqualInUI( const SmSym& rSymbol ) const
80 return m_aName == rSymbol.m_aName &&
81 m_aFace == rSymbol.m_aFace &&
82 m_cChar == rSymbol.m_cChar;
85 /**************************************************************************/
88 SmSymbolManager::SmSymbolManager()
90 m_bModified = false;
94 SmSymbolManager::SmSymbolManager(const SmSymbolManager& rSymbolSetManager)
96 m_aSymbols = rSymbolSetManager.m_aSymbols;
97 m_bModified = true;
101 SmSymbolManager::~SmSymbolManager()
106 SmSymbolManager& SmSymbolManager::operator = (const SmSymbolManager& rSymbolSetManager)
108 m_aSymbols = rSymbolSetManager.m_aSymbols;
109 m_bModified = true;
110 return *this;
114 SmSym *SmSymbolManager::GetSymbolByName(const OUString& rSymbolName)
116 SmSym *pRes = nullptr;
117 SymbolMap_t::iterator aIt( m_aSymbols.find( rSymbolName ) );
118 if (aIt != m_aSymbols.end())
119 pRes = &aIt->second;
120 return pRes;
124 const SymbolPtrVec_t SmSymbolManager::GetSymbols() const
126 SymbolPtrVec_t aRes;
127 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
128 for ( ; aIt != m_aSymbols.end(); ++aIt)
129 aRes.push_back( &aIt->second );
130 // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
131 return aRes;
135 bool SmSymbolManager::AddOrReplaceSymbol( const SmSym &rSymbol, bool bForceChange )
137 bool bAdded = false;
139 const OUString& aSymbolName( rSymbol.GetName() );
140 if (!aSymbolName.isEmpty() && !rSymbol.GetSymbolSetName().isEmpty())
142 const SmSym *pFound = GetSymbolByName( aSymbolName );
143 const bool bSymbolConflict = pFound && !pFound->IsEqualInUI( rSymbol );
145 // avoid having the same symbol name twice but with different symbols in use
146 if (!pFound || bForceChange)
148 m_aSymbols[ aSymbolName ] = rSymbol;
149 bAdded = true;
151 else if (pFound && !bForceChange && bSymbolConflict)
153 // TODO: to solve this a document owned symbol manager would be required ...
154 SAL_WARN("starmath", "symbol conflict, different symbol with same name found!");
155 // symbols in all formulas. A copy of the global one would be needed here
156 // and then the new symbol has to be forcefully applied. This would keep
157 // the current formula intact but will leave the set of symbols in the
158 // global symbol manager somewhat to chance.
161 OSL_ENSURE( bAdded, "failed to add symbol" );
162 if (bAdded)
163 m_bModified = true;
164 OSL_ENSURE( bAdded || (pFound && !bSymbolConflict), "AddOrReplaceSymbol: unresolved symbol conflict" );
167 return bAdded;
171 void SmSymbolManager::RemoveSymbol( const OUString & rSymbolName )
173 if (!rSymbolName.isEmpty())
175 size_t nOldSize = m_aSymbols.size();
176 m_aSymbols.erase( rSymbolName );
177 m_bModified = nOldSize != m_aSymbols.size();
182 std::set< OUString > SmSymbolManager::GetSymbolSetNames() const
184 std::set< OUString > aRes;
185 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
186 for ( ; aIt != m_aSymbols.end(); ++aIt )
187 aRes.insert( aIt->second.GetSymbolSetName() );
188 return aRes;
192 const SymbolPtrVec_t SmSymbolManager::GetSymbolSet( const OUString& rSymbolSetName )
194 SymbolPtrVec_t aRes;
195 if (!rSymbolSetName.isEmpty())
197 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
198 for ( ; aIt != m_aSymbols.end(); ++aIt )
200 if (aIt->second.GetSymbolSetName() == rSymbolSetName)
201 aRes.push_back( &aIt->second );
204 return aRes;
208 void SmSymbolManager::Load()
210 std::vector< SmSym > aSymbols;
211 SmMathConfig &rCfg = *SM_MOD()->GetConfig();
212 rCfg.GetSymbols( aSymbols );
213 size_t nSymbolCount = aSymbols.size();
215 m_aSymbols.clear();
216 for (size_t i = 0; i < nSymbolCount; ++i)
218 const SmSym &rSym = aSymbols[i];
219 OSL_ENSURE( !rSym.GetName().isEmpty(), "symbol without name!" );
220 if (!rSym.GetName().isEmpty())
221 AddOrReplaceSymbol( rSym );
223 m_bModified = true;
225 if (0 == nSymbolCount)
227 SAL_WARN("starmath", "no symbol set found");
228 m_bModified = false;
231 // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
232 const OUString aGreekSymbolSetName(SmLocalizedSymbolData::GetUiSymbolSetName("Greek"));
233 const SymbolPtrVec_t aGreekSymbols( GetSymbolSet( aGreekSymbolSetName ) );
234 OUString aSymbolSetName('i');
235 aSymbolSetName += aGreekSymbolSetName;
236 size_t nSymbols = aGreekSymbols.size();
237 for (size_t i = 0; i < nSymbols; ++i)
239 // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
240 const SmSym &rSym = *aGreekSymbols[i];
241 vcl::Font aFont( rSym.GetFace() );
242 OSL_ENSURE( aFont.GetItalic() == ITALIC_NONE, "expected Font with ITALIC_NONE, failed." );
243 aFont.SetItalic( ITALIC_NORMAL );
244 OUString aSymbolName('i');
245 aSymbolName += rSym.GetName();
246 SmSym aSymbol( aSymbolName, aFont, rSym.GetCharacter(),
247 aSymbolSetName, true /*bIsPredefined*/ );
249 AddOrReplaceSymbol( aSymbol );
253 void SmSymbolManager::Save()
255 if (m_bModified)
257 SmMathConfig &rCfg = *SM_MOD()->GetConfig();
259 // prepare to skip symbols from iGreek on saving
260 OUString aSymbolSetName('i');
261 aSymbolSetName += SmLocalizedSymbolData::GetUiSymbolSetName("Greek");
263 SymbolPtrVec_t aTmp( GetSymbols() );
264 std::vector< SmSym > aSymbols;
265 for (const SmSym* i : aTmp)
267 // skip symbols from iGreek set since those symbols always get added
268 // by computational means in SmSymbolManager::Load
269 if (i->GetSymbolSetName() != aSymbolSetName)
270 aSymbols.push_back( *i );
272 rCfg.SetSymbols( aSymbols );
274 m_bModified = false;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */