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 .
21 #include <utility.hxx>
22 #include <cfgitem.hxx>
24 #include <sal/log.hxx>
25 #include <osl/diagnose.h>
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 SM_MOD()->GetSymbolManager().SetModified(true);
76 bool SmSym::IsEqualInUI( const SmSym
& rSymbol
) const
78 return m_aName
== rSymbol
.m_aName
&&
79 m_aFace
== rSymbol
.m_aFace
&&
80 m_cChar
== rSymbol
.m_cChar
;
83 /**************************************************************************/
86 SmSymbolManager::SmSymbolManager()
92 SmSymbolManager::SmSymbolManager(const SmSymbolManager
& rSymbolSetManager
)
94 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
99 SmSymbolManager::~SmSymbolManager()
104 SmSymbolManager
& SmSymbolManager::operator = (const SmSymbolManager
& rSymbolSetManager
)
106 m_aSymbols
= rSymbolSetManager
.m_aSymbols
;
112 SmSym
*SmSymbolManager::GetSymbolByName(const OUString
& rSymbolName
)
114 SmSym
*pRes
= nullptr;
115 SymbolMap_t::iterator
aIt( m_aSymbols
.find( rSymbolName
) );
116 if (aIt
!= m_aSymbols
.end())
122 SymbolPtrVec_t
SmSymbolManager::GetSymbols() const
125 aRes
.reserve(m_aSymbols
.size());
126 for (const auto& rEntry
: m_aSymbols
)
127 aRes
.push_back( &rEntry
.second
);
128 // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
133 bool SmSymbolManager::AddOrReplaceSymbol( const SmSym
&rSymbol
, bool bForceChange
)
137 const OUString
& aSymbolName( rSymbol
.GetName() );
138 if (!aSymbolName
.isEmpty() && !rSymbol
.GetSymbolSetName().isEmpty())
140 const SmSym
*pFound
= GetSymbolByName( aSymbolName
);
141 const bool bSymbolConflict
= pFound
&& !pFound
->IsEqualInUI( rSymbol
);
143 // avoid having the same symbol name twice but with different symbols in use
144 if (!pFound
|| bForceChange
)
146 m_aSymbols
[ aSymbolName
] = rSymbol
;
149 else if (bSymbolConflict
)
151 // TODO: to solve this a document owned symbol manager would be required ...
152 SAL_WARN("starmath", "symbol conflict, different symbol with same name found!");
153 // symbols in all formulas. A copy of the global one would be needed here
154 // and then the new symbol has to be forcefully applied. This would keep
155 // the current formula intact but will leave the set of symbols in the
156 // global symbol manager somewhat to chance.
159 OSL_ENSURE( bAdded
, "failed to add symbol" );
162 OSL_ENSURE( bAdded
|| (pFound
&& !bSymbolConflict
), "AddOrReplaceSymbol: unresolved symbol conflict" );
169 void SmSymbolManager::RemoveSymbol( const OUString
& rSymbolName
)
171 if (!rSymbolName
.isEmpty())
173 size_t nOldSize
= m_aSymbols
.size();
174 m_aSymbols
.erase( rSymbolName
);
175 m_bModified
= nOldSize
!= m_aSymbols
.size();
180 std::set
< OUString
> SmSymbolManager::GetSymbolSetNames() const
182 std::set
< OUString
> aRes
;
183 for (const auto& rEntry
: m_aSymbols
)
184 aRes
.insert( rEntry
.second
.GetSymbolSetName() );
189 SymbolPtrVec_t
SmSymbolManager::GetSymbolSet( std::u16string_view rSymbolSetName
)
192 if (!rSymbolSetName
.empty())
194 for (const auto& rEntry
: m_aSymbols
)
196 if (rEntry
.second
.GetSymbolSetName() == rSymbolSetName
)
197 aRes
.push_back( &rEntry
.second
);
204 void SmSymbolManager::Load()
206 std::vector
< SmSym
> aSymbols
;
207 SmMathConfig
&rCfg
= *SM_MOD()->GetConfig();
208 rCfg
.GetSymbols( aSymbols
);
209 size_t nSymbolCount
= aSymbols
.size();
212 for (size_t i
= 0; i
< nSymbolCount
; ++i
)
214 const SmSym
&rSym
= aSymbols
[i
];
215 OSL_ENSURE( !rSym
.GetName().isEmpty(), "symbol without name!" );
216 if (!rSym
.GetName().isEmpty())
217 AddOrReplaceSymbol( rSym
);
221 if (0 == nSymbolCount
)
223 SAL_WARN("starmath", "no symbol set found");
227 // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
228 const OUString
aGreekSymbolSetName(SmLocalizedSymbolData::GetUiSymbolSetName(u
"Greek"));
229 const SymbolPtrVec_t
aGreekSymbols( GetSymbolSet( aGreekSymbolSetName
) );
230 OUString aSymbolSetName
= "i" + aGreekSymbolSetName
;
231 size_t nSymbols
= aGreekSymbols
.size();
232 for (size_t i
= 0; i
< nSymbols
; ++i
)
234 // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
235 const SmSym
&rSym
= *aGreekSymbols
[i
];
236 vcl::Font
aFont( rSym
.GetFace() );
237 OSL_ENSURE( aFont
.GetItalic() == ITALIC_NONE
, "expected Font with ITALIC_NONE, failed." );
238 aFont
.SetItalic( ITALIC_NORMAL
);
239 OUString aSymbolName
= "i" + rSym
.GetName();
240 SmSym
aSymbol( aSymbolName
, aFont
, rSym
.GetCharacter(),
241 aSymbolSetName
, true /*bIsPredefined*/ );
243 AddOrReplaceSymbol( aSymbol
);
247 void SmSymbolManager::Save()
252 SmMathConfig
&rCfg
= *SM_MOD()->GetConfig();
254 // prepare to skip symbols from iGreek on saving
255 OUString aSymbolSetName
= "i" +
256 SmLocalizedSymbolData::GetUiSymbolSetName(u
"Greek");
258 SymbolPtrVec_t
aTmp( GetSymbols() );
259 std::vector
< SmSym
> aSymbols
;
260 for (const SmSym
* i
: aTmp
)
262 // skip symbols from iGreek set since those symbols always get added
263 // by computational means in SmSymbolManager::Load
264 if (i
->GetSymbolSetName() != aSymbolSetName
)
265 aSymbols
.push_back( *i
);
267 rCfg
.SetSymbols( aSymbols
);
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */