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 .
23 #include <utility.hxx>
24 #include "cfgitem.hxx"
29 m_aName(OUString("unknown")),
30 m_aSetName(OUString("unknown")),
34 m_aExportName
= m_aName
;
35 m_aFace
.SetTransparent(true);
36 m_aFace
.SetAlignment(ALIGN_BASELINE
);
40 SmSym::SmSym(const SmSym
& 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
;
52 m_aFace
.SetTransparent(true);
53 m_aFace
.SetAlignment(ALIGN_BASELINE
);
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();
72 pSymSetManager
->SetModified(true);
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()
94 SmSymbolManager::SmSymbolManager(const SmSymbolManager
& rSymbolSetManager
)
96 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
101 SmSymbolManager::~SmSymbolManager()
106 SmSymbolManager
& SmSymbolManager::operator = (const SmSymbolManager
& rSymbolSetManager
)
108 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
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())
124 const SymbolPtrVec_t
SmSymbolManager::GetSymbols() const
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 " );
135 bool SmSymbolManager::AddOrReplaceSymbol( const SmSym
&rSymbol
, bool bForceChange
)
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
;
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" );
164 OSL_ENSURE( bAdded
|| (pFound
&& !bSymbolConflict
), "AddOrReplaceSymbol: unresolved symbol conflict" );
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() );
192 const SymbolPtrVec_t
SmSymbolManager::GetSymbolSet( const OUString
& rSymbolSetName
)
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
);
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();
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
);
225 if (0 == nSymbolCount
)
227 SAL_WARN("starmath", "no symbol set found");
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()
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
);
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */