Bump version to 21.06.18.1
[LibreOffice.git] / starmath / source / cfgitem.cxx
blobf3c0ec3e99b8daf899f4f07d77aeeccc5117a140
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 <cassert>
21 #include <memory>
22 #include <vector>
24 #include <svl/itemset.hxx>
25 #include <svl/intitem.hxx>
26 #include <svl/itempool.hxx>
27 #include <svl/eitem.hxx>
28 #include <svl/languageoptions.hxx>
29 #include <vcl/outdev.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 #include <sal/log.hxx>
33 #include <osl/diagnose.h>
34 #include <i18nlangtag/languagetag.hxx>
36 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <officecfg/Office/Math.hxx>
39 #include "cfgitem.hxx"
41 #include <starmath.hrc>
42 #include <smmod.hxx>
43 #include <symbol.hxx>
44 #include <format.hxx>
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::beans;
49 #define SYMBOL_LIST "SymbolList"
50 #define FONT_FORMAT_LIST "FontFormatList"
52 static Sequence< OUString > lcl_GetFontPropertyNames()
54 return Sequence< OUString > {
55 "Name",
56 "CharSet",
57 "Family",
58 "Pitch",
59 "Weight",
60 "Italic"
64 static Sequence< OUString > lcl_GetSymbolPropertyNames()
66 return Sequence< OUString > {
67 "Char",
68 "Set",
69 "Predefined",
70 "FontFormatId"
74 static Sequence< OUString > lcl_GetFormatPropertyNames()
76 //! Beware of order according to *_BEGIN *_END defines in format.hxx !
77 //! see respective load/save routines here
78 return Sequence< OUString > {
79 "StandardFormat/Textmode",
80 "StandardFormat/GreekCharStyle",
81 "StandardFormat/ScaleNormalBracket",
82 "StandardFormat/HorizontalAlignment",
83 "StandardFormat/BaseSize",
84 "StandardFormat/TextSize",
85 "StandardFormat/IndexSize",
86 "StandardFormat/FunctionSize",
87 "StandardFormat/OperatorSize",
88 "StandardFormat/LimitsSize",
89 "StandardFormat/Distance/Horizontal",
90 "StandardFormat/Distance/Vertical",
91 "StandardFormat/Distance/Root",
92 "StandardFormat/Distance/SuperScript",
93 "StandardFormat/Distance/SubScript",
94 "StandardFormat/Distance/Numerator",
95 "StandardFormat/Distance/Denominator",
96 "StandardFormat/Distance/Fraction",
97 "StandardFormat/Distance/StrokeWidth",
98 "StandardFormat/Distance/UpperLimit",
99 "StandardFormat/Distance/LowerLimit",
100 "StandardFormat/Distance/BracketSize",
101 "StandardFormat/Distance/BracketSpace",
102 "StandardFormat/Distance/MatrixRow",
103 "StandardFormat/Distance/MatrixColumn",
104 "StandardFormat/Distance/OrnamentSize",
105 "StandardFormat/Distance/OrnamentSpace",
106 "StandardFormat/Distance/OperatorSize",
107 "StandardFormat/Distance/OperatorSpace",
108 "StandardFormat/Distance/LeftSpace",
109 "StandardFormat/Distance/RightSpace",
110 "StandardFormat/Distance/TopSpace",
111 "StandardFormat/Distance/BottomSpace",
112 "StandardFormat/Distance/NormalBracketSize",
113 "StandardFormat/VariableFont",
114 "StandardFormat/FunctionFont",
115 "StandardFormat/NumberFont",
116 "StandardFormat/TextFont",
117 "StandardFormat/SerifFont",
118 "StandardFormat/SansFont",
119 "StandardFormat/FixedFont"
123 struct SmCfgOther
125 SmPrintSize ePrintSize;
126 sal_uInt16 nPrintZoomFactor;
127 bool bPrintTitle;
128 bool bPrintFormulaText;
129 bool bPrintFrame;
130 bool bIsSaveOnlyUsedSymbols;
131 bool bIsAutoCloseBrackets;
132 bool bIgnoreSpacesRight;
133 bool bToolboxVisible;
134 bool bAutoRedraw;
135 bool bFormulaCursor;
137 SmCfgOther();
141 SmCfgOther::SmCfgOther()
142 : ePrintSize(PRINT_SIZE_NORMAL)
143 , nPrintZoomFactor(100)
144 , bPrintTitle(true)
145 , bPrintFormulaText(true)
146 , bPrintFrame(true)
147 , bIsSaveOnlyUsedSymbols(true)
148 , bIsAutoCloseBrackets(true)
149 , bIgnoreSpacesRight(true)
150 , bToolboxVisible(true)
151 , bAutoRedraw(true)
152 , bFormulaCursor(true)
157 SmFontFormat::SmFontFormat()
158 : aName(FONTNAME_MATH)
159 , nCharSet(RTL_TEXTENCODING_UNICODE)
160 , nFamily(FAMILY_DONTKNOW)
161 , nPitch(PITCH_DONTKNOW)
162 , nWeight(WEIGHT_DONTKNOW)
163 , nItalic(ITALIC_NONE)
168 SmFontFormat::SmFontFormat( const vcl::Font &rFont )
169 : aName(rFont.GetFamilyName())
170 , nCharSet(static_cast<sal_Int16>(rFont.GetCharSet()))
171 , nFamily(static_cast<sal_Int16>(rFont.GetFamilyType()))
172 , nPitch(static_cast<sal_Int16>(rFont.GetPitch()))
173 , nWeight(static_cast<sal_Int16>(rFont.GetWeight()))
174 , nItalic(static_cast<sal_Int16>(rFont.GetItalic()))
179 vcl::Font SmFontFormat::GetFont() const
181 vcl::Font aRes;
182 aRes.SetFamilyName( aName );
183 aRes.SetCharSet( static_cast<rtl_TextEncoding>(nCharSet) );
184 aRes.SetFamily( static_cast<FontFamily>(nFamily) );
185 aRes.SetPitch( static_cast<FontPitch>(nPitch) );
186 aRes.SetWeight( static_cast<FontWeight>(nWeight) );
187 aRes.SetItalic( static_cast<FontItalic>(nItalic) );
188 return aRes;
192 bool SmFontFormat::operator == ( const SmFontFormat &rFntFmt ) const
194 return aName == rFntFmt.aName &&
195 nCharSet == rFntFmt.nCharSet &&
196 nFamily == rFntFmt.nFamily &&
197 nPitch == rFntFmt.nPitch &&
198 nWeight == rFntFmt.nWeight &&
199 nItalic == rFntFmt.nItalic;
203 SmFntFmtListEntry::SmFntFmtListEntry( const OUString &rId, const SmFontFormat &rFntFmt ) :
204 aId (rId),
205 aFntFmt (rFntFmt)
210 SmFontFormatList::SmFontFormatList()
211 : bModified(false)
216 void SmFontFormatList::Clear()
218 if (!aEntries.empty())
220 aEntries.clear();
221 SetModified( true );
226 void SmFontFormatList::AddFontFormat( const OUString &rFntFmtId,
227 const SmFontFormat &rFntFmt )
229 const SmFontFormat *pFntFmt = GetFontFormat( rFntFmtId );
230 OSL_ENSURE( !pFntFmt, "FontFormatId already exists" );
231 if (!pFntFmt)
233 SmFntFmtListEntry aEntry( rFntFmtId, rFntFmt );
234 aEntries.push_back( aEntry );
235 SetModified( true );
240 void SmFontFormatList::RemoveFontFormat( const OUString &rFntFmtId )
243 // search for entry
244 for (size_t i = 0; i < aEntries.size(); ++i)
246 if (aEntries[i].aId == rFntFmtId)
248 // remove entry if found
249 aEntries.erase( aEntries.begin() + i );
250 SetModified( true );
251 break;
257 const SmFontFormat * SmFontFormatList::GetFontFormat( const OUString &rFntFmtId ) const
259 const SmFontFormat *pRes = nullptr;
261 for (const auto & rEntry : aEntries)
263 if (rEntry.aId == rFntFmtId)
265 pRes = &rEntry.aFntFmt;
266 break;
270 return pRes;
274 const SmFontFormat * SmFontFormatList::GetFontFormat( size_t nPos ) const
276 const SmFontFormat *pRes = nullptr;
277 if (nPos < aEntries.size())
278 pRes = &aEntries[nPos].aFntFmt;
279 return pRes;
283 OUString SmFontFormatList::GetFontFormatId( const SmFontFormat &rFntFmt ) const
285 OUString aRes;
287 for (const auto & rEntry : aEntries)
289 if (rEntry.aFntFmt == rFntFmt)
291 aRes = rEntry.aId;
292 break;
296 return aRes;
300 OUString SmFontFormatList::GetFontFormatId( const SmFontFormat &rFntFmt, bool bAdd )
302 OUString aRes( GetFontFormatId( rFntFmt) );
303 if (aRes.isEmpty() && bAdd)
305 aRes = GetNewFontFormatId();
306 AddFontFormat( aRes, rFntFmt );
308 return aRes;
312 OUString SmFontFormatList::GetFontFormatId( size_t nPos ) const
314 OUString aRes;
315 if (nPos < aEntries.size())
316 aRes = aEntries[nPos].aId;
317 return aRes;
321 OUString SmFontFormatList::GetNewFontFormatId() const
323 // returns first unused FormatId
325 sal_Int32 nCnt = GetCount();
326 for (sal_Int32 i = 1; i <= nCnt + 1; ++i)
328 OUString aTmpId = "Id" + OUString::number(i);
329 if (!GetFontFormat(aTmpId))
330 return aTmpId;
332 OSL_ENSURE( false, "failed to create new FontFormatId" );
334 return OUString();
338 SmMathConfig::SmMathConfig() :
339 ConfigItem("Office.Math")
340 , pFormat()
341 , pOther()
342 , pFontFormatList()
343 , pSymbolMgr()
344 , bIsOtherModified(false)
345 , bIsFormatModified(false)
350 SmMathConfig::~SmMathConfig()
352 Save();
356 void SmMathConfig::SetOtherModified( bool bVal )
358 bIsOtherModified = bVal;
362 void SmMathConfig::SetFormatModified( bool bVal )
364 bIsFormatModified = bVal;
368 void SmMathConfig::ReadSymbol( SmSym &rSymbol,
369 const OUString &rSymbolName,
370 const OUString &rBaseNode ) const
372 Sequence< OUString > aNames = lcl_GetSymbolPropertyNames();
373 sal_Int32 nProps = aNames.getLength();
375 OUString aDelim( "/" );
376 for (auto& rName : aNames)
377 rName = rBaseNode + aDelim + rSymbolName + aDelim + rName;
379 const Sequence< Any > aValues = const_cast<SmMathConfig*>(this)->GetProperties(aNames);
381 if (!(nProps && aValues.getLength() == nProps))
382 return;
384 const Any * pValue = aValues.getConstArray();
385 vcl::Font aFont;
386 sal_UCS4 cChar = '\0';
387 OUString aSet;
388 bool bPredefined = false;
390 OUString aTmpStr;
391 sal_Int32 nTmp32 = 0;
392 bool bTmp = false;
394 bool bOK = true;
395 if (pValue->hasValue() && (*pValue >>= nTmp32))
396 cChar = static_cast< sal_UCS4 >( nTmp32 );
397 else
398 bOK = false;
399 ++pValue;
400 if (pValue->hasValue() && (*pValue >>= aTmpStr))
401 aSet = aTmpStr;
402 else
403 bOK = false;
404 ++pValue;
405 if (pValue->hasValue() && (*pValue >>= bTmp))
406 bPredefined = bTmp;
407 else
408 bOK = false;
409 ++pValue;
410 if (pValue->hasValue() && (*pValue >>= aTmpStr))
412 const SmFontFormat *pFntFmt = GetFontFormatList().GetFontFormat( aTmpStr );
413 OSL_ENSURE( pFntFmt, "unknown FontFormat" );
414 if (pFntFmt)
415 aFont = pFntFmt->GetFont();
417 else
418 bOK = false;
419 ++pValue;
421 if (bOK)
423 OUString aUiName( rSymbolName );
424 OUString aUiSetName( aSet );
425 if (bPredefined)
427 OUString aTmp;
428 aTmp = SmLocalizedSymbolData::GetUiSymbolName( rSymbolName );
429 OSL_ENSURE( !aTmp.isEmpty(), "localized symbol-name not found" );
430 if (!aTmp.isEmpty())
431 aUiName = aTmp;
432 aTmp = SmLocalizedSymbolData::GetUiSymbolSetName( aSet );
433 OSL_ENSURE( !aTmp.isEmpty(), "localized symbolset-name not found" );
434 if (!aTmp.isEmpty())
435 aUiSetName = aTmp;
438 rSymbol = SmSym( aUiName, aFont, cChar, aUiSetName, bPredefined );
439 if (aUiName != rSymbolName)
440 rSymbol.SetExportName( rSymbolName );
442 else
444 SAL_WARN("starmath", "symbol read error");
449 SmSymbolManager & SmMathConfig::GetSymbolManager()
451 if (!pSymbolMgr)
453 pSymbolMgr.reset(new SmSymbolManager);
454 pSymbolMgr->Load();
456 return *pSymbolMgr;
460 void SmMathConfig::ImplCommit()
462 Save();
466 void SmMathConfig::Save()
468 SaveOther();
469 SaveFormat();
470 SaveFontFormatList();
474 void SmMathConfig::GetSymbols( std::vector< SmSym > &rSymbols ) const
476 Sequence< OUString > aNodes(const_cast<SmMathConfig*>(this)->GetNodeNames(SYMBOL_LIST));
477 const OUString *pNode = aNodes.getConstArray();
478 sal_Int32 nNodes = aNodes.getLength();
480 rSymbols.resize( nNodes );
481 for (auto& rSymbol : rSymbols)
483 ReadSymbol( rSymbol, *pNode++, SYMBOL_LIST );
488 void SmMathConfig::SetSymbols( const std::vector< SmSym > &rNewSymbols )
490 auto nCount = sal::static_int_cast<sal_Int32>(rNewSymbols.size());
492 Sequence< OUString > aNames = lcl_GetSymbolPropertyNames();
493 const OUString *pNames = aNames.getConstArray();
494 sal_Int32 nSymbolProps = aNames.getLength();
496 Sequence< PropertyValue > aValues( nCount * nSymbolProps );
497 PropertyValue *pValues = aValues.getArray();
499 PropertyValue *pVal = pValues;
500 OUString aDelim( "/" );
501 for (const SmSym& rSymbol : rNewSymbols)
503 OUString aNodeNameDelim = SYMBOL_LIST +
504 aDelim +
505 rSymbol.GetExportName() +
506 aDelim;
508 const OUString *pName = pNames;
510 // Char
511 pVal->Name = aNodeNameDelim;
512 pVal->Name += *pName++;
513 pVal->Value <<= rSymbol.GetCharacter();
514 pVal++;
515 // Set
516 pVal->Name = aNodeNameDelim;
517 pVal->Name += *pName++;
518 OUString aTmp( rSymbol.GetSymbolSetName() );
519 if (rSymbol.IsPredefined())
520 aTmp = SmLocalizedSymbolData::GetExportSymbolSetName( aTmp );
521 pVal->Value <<= aTmp;
522 pVal++;
523 // Predefined
524 pVal->Name = aNodeNameDelim;
525 pVal->Name += *pName++;
526 pVal->Value <<= rSymbol.IsPredefined();
527 pVal++;
528 // FontFormatId
529 SmFontFormat aFntFmt( rSymbol.GetFace() );
530 OUString aFntFmtId( GetFontFormatList().GetFontFormatId( aFntFmt, true ) );
531 OSL_ENSURE( !aFntFmtId.isEmpty(), "FontFormatId not found" );
532 pVal->Name = aNodeNameDelim;
533 pVal->Name += *pName++;
534 pVal->Value <<= aFntFmtId;
535 pVal++;
537 OSL_ENSURE( pVal - pValues == sal::static_int_cast< ptrdiff_t >(nCount * nSymbolProps), "properties missing" );
538 ReplaceSetProperties( SYMBOL_LIST, aValues );
540 StripFontFormatList( rNewSymbols );
541 SaveFontFormatList();
545 SmFontFormatList & SmMathConfig::GetFontFormatList()
547 if (!pFontFormatList)
549 LoadFontFormatList();
551 return *pFontFormatList;
555 void SmMathConfig::LoadFontFormatList()
557 if (!pFontFormatList)
558 pFontFormatList.reset(new SmFontFormatList);
559 else
560 pFontFormatList->Clear();
562 const Sequence< OUString > aNodes( GetNodeNames( FONT_FORMAT_LIST ) );
564 for (const OUString& rNode : aNodes)
566 SmFontFormat aFntFmt;
567 ReadFontFormat( aFntFmt, rNode, FONT_FORMAT_LIST );
568 if (!pFontFormatList->GetFontFormat( rNode ))
569 pFontFormatList->AddFontFormat( rNode, aFntFmt );
571 pFontFormatList->SetModified( false );
575 void SmMathConfig::ReadFontFormat( SmFontFormat &rFontFormat,
576 const OUString &rSymbolName, const OUString &rBaseNode ) const
578 Sequence< OUString > aNames = lcl_GetFontPropertyNames();
579 sal_Int32 nProps = aNames.getLength();
581 OUString aDelim( "/" );
582 for (auto& rName : aNames)
583 rName = rBaseNode + aDelim + rSymbolName + aDelim + rName;
585 const Sequence< Any > aValues = const_cast<SmMathConfig*>(this)->GetProperties(aNames);
587 if (!(nProps && aValues.getLength() == nProps))
588 return;
590 const Any * pValue = aValues.getConstArray();
592 OUString aTmpStr;
593 sal_Int16 nTmp16 = 0;
595 bool bOK = true;
596 if (pValue->hasValue() && (*pValue >>= aTmpStr))
597 rFontFormat.aName = aTmpStr;
598 else
599 bOK = false;
600 ++pValue;
601 if (pValue->hasValue() && (*pValue >>= nTmp16))
602 rFontFormat.nCharSet = nTmp16; // 6.0 file-format GetSOLoadTextEncoding not needed
603 else
604 bOK = false;
605 ++pValue;
606 if (pValue->hasValue() && (*pValue >>= nTmp16))
607 rFontFormat.nFamily = nTmp16;
608 else
609 bOK = false;
610 ++pValue;
611 if (pValue->hasValue() && (*pValue >>= nTmp16))
612 rFontFormat.nPitch = nTmp16;
613 else
614 bOK = false;
615 ++pValue;
616 if (pValue->hasValue() && (*pValue >>= nTmp16))
617 rFontFormat.nWeight = nTmp16;
618 else
619 bOK = false;
620 ++pValue;
621 if (pValue->hasValue() && (*pValue >>= nTmp16))
622 rFontFormat.nItalic = nTmp16;
623 else
624 bOK = false;
625 ++pValue;
627 OSL_ENSURE( bOK, "read FontFormat failed" );
631 void SmMathConfig::SaveFontFormatList()
633 SmFontFormatList &rFntFmtList = GetFontFormatList();
635 if (!rFntFmtList.IsModified())
636 return;
638 Sequence< OUString > aNames = lcl_GetFontPropertyNames();
639 sal_Int32 nSymbolProps = aNames.getLength();
641 size_t nCount = rFntFmtList.GetCount();
643 Sequence< PropertyValue > aValues( nCount * nSymbolProps );
644 PropertyValue *pValues = aValues.getArray();
646 PropertyValue *pVal = pValues;
647 OUString aDelim( "/" );
648 for (size_t i = 0; i < nCount; ++i)
650 OUString aFntFmtId(rFntFmtList.GetFontFormatId(i));
651 const SmFontFormat *pFntFmt = rFntFmtList.GetFontFormat(i);
652 assert(pFntFmt);
653 const SmFontFormat aFntFmt(*pFntFmt);
655 OUString aNodeNameDelim = FONT_FORMAT_LIST +
656 aDelim +
657 aFntFmtId +
658 aDelim;
660 const OUString *pName = aNames.getConstArray();
662 // Name
663 pVal->Name = aNodeNameDelim;
664 pVal->Name += *pName++;
665 pVal->Value <<= aFntFmt.aName;
666 pVal++;
667 // CharSet
668 pVal->Name = aNodeNameDelim;
669 pVal->Name += *pName++;
670 pVal->Value <<= aFntFmt.nCharSet; // 6.0 file-format GetSOStoreTextEncoding not needed
671 pVal++;
672 // Family
673 pVal->Name = aNodeNameDelim;
674 pVal->Name += *pName++;
675 pVal->Value <<= aFntFmt.nFamily;
676 pVal++;
677 // Pitch
678 pVal->Name = aNodeNameDelim;
679 pVal->Name += *pName++;
680 pVal->Value <<= aFntFmt.nPitch;
681 pVal++;
682 // Weight
683 pVal->Name = aNodeNameDelim;
684 pVal->Name += *pName++;
685 pVal->Value <<= aFntFmt.nWeight;
686 pVal++;
687 // Italic
688 pVal->Name = aNodeNameDelim;
689 pVal->Name += *pName++;
690 pVal->Value <<= aFntFmt.nItalic;
691 pVal++;
693 OSL_ENSURE( sal::static_int_cast<size_t>(pVal - pValues) == nCount * nSymbolProps, "properties missing" );
694 ReplaceSetProperties( FONT_FORMAT_LIST, aValues );
696 rFntFmtList.SetModified( false );
700 void SmMathConfig::StripFontFormatList( const std::vector< SmSym > &rSymbols )
702 size_t i;
704 // build list of used font-formats only
705 //!! font-format IDs may be different !!
706 SmFontFormatList aUsedList;
707 for (i = 0; i < rSymbols.size(); ++i)
709 OSL_ENSURE( rSymbols[i].GetName().getLength() > 0, "non named symbol" );
710 aUsedList.GetFontFormatId( SmFontFormat( rSymbols[i].GetFace() ) , true );
712 const SmFormat & rStdFmt = GetStandardFormat();
713 for (i = FNT_BEGIN; i <= FNT_END; ++i)
715 aUsedList.GetFontFormatId( SmFontFormat( rStdFmt.GetFont( i ) ) , true );
718 // remove unused font-formats from list
719 SmFontFormatList &rFntFmtList = GetFontFormatList();
720 size_t nCnt = rFntFmtList.GetCount();
721 std::unique_ptr<SmFontFormat[]> pTmpFormat(new SmFontFormat[ nCnt ]);
722 std::unique_ptr<OUString[]> pId(new OUString[ nCnt ]);
723 size_t k;
724 for (k = 0; k < nCnt; ++k)
726 pTmpFormat[k] = *rFntFmtList.GetFontFormat( k );
727 pId[k] = rFntFmtList.GetFontFormatId( k );
729 for (k = 0; k < nCnt; ++k)
731 if (aUsedList.GetFontFormatId( pTmpFormat[k] ).isEmpty())
733 rFntFmtList.RemoveFontFormat( pId[k] );
739 void SmMathConfig::LoadOther()
741 if (!pOther)
742 pOther.reset(new SmCfgOther);
744 pOther->bPrintTitle = officecfg::Office::Math::Print::Title::get();
745 pOther->bPrintFormulaText = officecfg::Office::Math::Print::FormulaText::get();
746 pOther->bPrintFrame = officecfg::Office::Math::Print::Frame::get();
747 pOther->ePrintSize = static_cast<SmPrintSize>(officecfg::Office::Math::Print::Size::get());
748 pOther->nPrintZoomFactor = officecfg::Office::Math::Print::ZoomFactor::get();
749 pOther->bIsSaveOnlyUsedSymbols = officecfg::Office::Math::LoadSave::IsSaveOnlyUsedSymbols::get();
750 pOther->bIsAutoCloseBrackets = officecfg::Office::Math::Misc::AutoCloseBrackets::get();
751 pOther->bIgnoreSpacesRight = officecfg::Office::Math::Misc::IgnoreSpacesRight::get();
752 pOther->bToolboxVisible = officecfg::Office::Math::View::ToolboxVisible::get();
753 pOther->bAutoRedraw = officecfg::Office::Math::View::AutoRedraw::get();
754 pOther->bFormulaCursor = officecfg::Office::Math::View::FormulaCursor::get();
755 SetOtherModified( false );
759 void SmMathConfig::SaveOther()
761 if (!pOther || !IsOtherModified())
762 return;
764 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
766 officecfg::Office::Math::Print::Title::set(pOther->bPrintTitle, batch);
767 officecfg::Office::Math::Print::FormulaText::set(pOther->bPrintFormulaText, batch);
768 officecfg::Office::Math::Print::Frame::set(pOther->bPrintFrame, batch);
769 officecfg::Office::Math::Print::Size::set(pOther->ePrintSize, batch);
770 officecfg::Office::Math::Print::ZoomFactor::set(pOther->nPrintZoomFactor, batch);
771 officecfg::Office::Math::LoadSave::IsSaveOnlyUsedSymbols::set(pOther->bIsSaveOnlyUsedSymbols, batch);
772 officecfg::Office::Math::Misc::AutoCloseBrackets::set(pOther->bIsAutoCloseBrackets, batch);
773 officecfg::Office::Math::Misc::IgnoreSpacesRight::set(pOther->bIgnoreSpacesRight, batch);
774 officecfg::Office::Math::View::ToolboxVisible::set(pOther->bToolboxVisible, batch);
775 officecfg::Office::Math::View::AutoRedraw::set(pOther->bAutoRedraw, batch);
776 officecfg::Office::Math::View::FormulaCursor::set(pOther->bFormulaCursor, batch);
778 batch->commit();
779 SetOtherModified( false );
782 namespace {
784 // Latin default-fonts
785 const DefaultFontType aLatinDefFnts[FNT_END] =
787 DefaultFontType::SERIF, // FNT_VARIABLE
788 DefaultFontType::SERIF, // FNT_FUNCTION
789 DefaultFontType::SERIF, // FNT_NUMBER
790 DefaultFontType::SERIF, // FNT_TEXT
791 DefaultFontType::SERIF, // FNT_SERIF
792 DefaultFontType::SANS, // FNT_SANS
793 DefaultFontType::FIXED // FNT_FIXED
794 //OpenSymbol, // FNT_MATH
797 // CJK default-fonts
798 //! we use non-asian fonts for variables, functions and numbers since they
799 //! look better and even in asia only latin letters will be used for those.
800 //! At least that's what I was told...
801 const DefaultFontType aCJKDefFnts[FNT_END] =
803 DefaultFontType::SERIF, // FNT_VARIABLE
804 DefaultFontType::SERIF, // FNT_FUNCTION
805 DefaultFontType::SERIF, // FNT_NUMBER
806 DefaultFontType::CJK_TEXT, // FNT_TEXT
807 DefaultFontType::CJK_TEXT, // FNT_SERIF
808 DefaultFontType::CJK_DISPLAY, // FNT_SANS
809 DefaultFontType::CJK_TEXT // FNT_FIXED
810 //OpenSymbol, // FNT_MATH
813 // CTL default-fonts
814 const DefaultFontType aCTLDefFnts[FNT_END] =
816 DefaultFontType::CTL_TEXT, // FNT_VARIABLE
817 DefaultFontType::CTL_TEXT, // FNT_FUNCTION
818 DefaultFontType::CTL_TEXT, // FNT_NUMBER
819 DefaultFontType::CTL_TEXT, // FNT_TEXT
820 DefaultFontType::CTL_TEXT, // FNT_SERIF
821 DefaultFontType::CTL_TEXT, // FNT_SANS
822 DefaultFontType::CTL_TEXT // FNT_FIXED
823 //OpenSymbol, // FNT_MATH
827 OUString lcl_GetDefaultFontName( LanguageType nLang, sal_uInt16 nIdent )
829 assert(nIdent < FNT_END);
830 const DefaultFontType *pTable;
831 switch ( SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ) )
833 case SvtScriptType::LATIN : pTable = aLatinDefFnts; break;
834 case SvtScriptType::ASIAN : pTable = aCJKDefFnts; break;
835 case SvtScriptType::COMPLEX : pTable = aCTLDefFnts; break;
836 default :
837 pTable = aLatinDefFnts;
838 SAL_WARN("starmath", "unknown script-type");
841 return OutputDevice::GetDefaultFont(pTable[ nIdent ], nLang,
842 GetDefaultFontFlags::OnlyOne ).GetFamilyName();
848 void SmMathConfig::LoadFormat()
850 if (!pFormat)
851 pFormat.reset(new SmFormat);
854 Sequence< OUString > aNames = lcl_GetFormatPropertyNames();
856 sal_Int32 nProps = aNames.getLength();
858 Sequence< Any > aValues( GetProperties( aNames ) );
859 if (!(nProps && aValues.getLength() == nProps))
860 return;
862 const Any *pValues = aValues.getConstArray();
863 const Any *pVal = pValues;
865 OUString aTmpStr;
866 sal_Int16 nTmp16 = 0;
867 bool bTmp = false;
869 // StandardFormat/Textmode
870 if (pVal->hasValue() && (*pVal >>= bTmp))
871 pFormat->SetTextmode( bTmp );
872 ++pVal;
873 // StandardFormat/GreekCharStyle
874 if (pVal->hasValue() && (*pVal >>= nTmp16))
875 pFormat->SetGreekCharStyle( nTmp16 );
876 ++pVal;
877 // StandardFormat/ScaleNormalBracket
878 if (pVal->hasValue() && (*pVal >>= bTmp))
879 pFormat->SetScaleNormalBrackets( bTmp );
880 ++pVal;
881 // StandardFormat/HorizontalAlignment
882 if (pVal->hasValue() && (*pVal >>= nTmp16))
883 pFormat->SetHorAlign( static_cast<SmHorAlign>(nTmp16) );
884 ++pVal;
885 // StandardFormat/BaseSize
886 if (pVal->hasValue() && (*pVal >>= nTmp16))
887 pFormat->SetBaseSize( Size(0, SmPtsTo100th_mm( nTmp16 )) );
888 ++pVal;
890 sal_uInt16 i;
891 for (i = SIZ_BEGIN; i <= SIZ_END; ++i)
893 if (pVal->hasValue() && (*pVal >>= nTmp16))
894 pFormat->SetRelSize( i, nTmp16 );
895 ++pVal;
898 for (i = DIS_BEGIN; i <= DIS_END; ++i)
900 if (pVal->hasValue() && (*pVal >>= nTmp16))
901 pFormat->SetDistance( i, nTmp16 );
902 ++pVal;
905 LanguageType nLang = Application::GetSettings().GetUILanguageTag().getLanguageType();
906 for (i = FNT_BEGIN; i < FNT_END; ++i)
908 vcl::Font aFnt;
909 bool bUseDefaultFont = true;
910 if (pVal->hasValue() && (*pVal >>= aTmpStr))
912 bUseDefaultFont = aTmpStr.isEmpty();
913 if (bUseDefaultFont)
915 aFnt = pFormat->GetFont( i );
916 aFnt.SetFamilyName( lcl_GetDefaultFontName( nLang, i ) );
918 else
920 const SmFontFormat *pFntFmt = GetFontFormatList().GetFontFormat( aTmpStr );
921 OSL_ENSURE( pFntFmt, "unknown FontFormat" );
922 if (pFntFmt)
923 aFnt = pFntFmt->GetFont();
926 ++pVal;
928 aFnt.SetFontSize( pFormat->GetBaseSize() );
929 pFormat->SetFont( i, aFnt, bUseDefaultFont );
932 OSL_ENSURE( pVal - pValues == nProps, "property mismatch" );
933 SetFormatModified( false );
937 void SmMathConfig::SaveFormat()
939 if (!pFormat || !IsFormatModified())
940 return;
942 const Sequence< OUString > aNames = lcl_GetFormatPropertyNames();
943 sal_Int32 nProps = aNames.getLength();
945 Sequence< Any > aValues( nProps );
946 Any *pValues = aValues.getArray();
947 Any *pValue = pValues;
949 // StandardFormat/Textmode
950 *pValue++ <<= pFormat->IsTextmode();
951 // StandardFormat/GreekCharStyle
952 *pValue++ <<= pFormat->GetGreekCharStyle();
953 // StandardFormat/ScaleNormalBracket
954 *pValue++ <<= pFormat->IsScaleNormalBrackets();
955 // StandardFormat/HorizontalAlignment
956 *pValue++ <<= static_cast<sal_Int16>(pFormat->GetHorAlign());
957 // StandardFormat/BaseSize
958 *pValue++ <<= static_cast<sal_Int16>(SmRoundFraction( Sm100th_mmToPts(
959 pFormat->GetBaseSize().Height() ) ));
961 sal_uInt16 i;
962 for (i = SIZ_BEGIN; i <= SIZ_END; ++i)
963 *pValue++ <<= static_cast<sal_Int16>(pFormat->GetRelSize( i ));
965 for (i = DIS_BEGIN; i <= DIS_END; ++i)
966 *pValue++ <<= static_cast<sal_Int16>(pFormat->GetDistance( i ));
968 for (i = FNT_BEGIN; i < FNT_END; ++i)
970 OUString aFntFmtId;
972 if (!pFormat->IsDefaultFont( i ))
974 SmFontFormat aFntFmt( pFormat->GetFont( i ) );
975 aFntFmtId = GetFontFormatList().GetFontFormatId( aFntFmt, true );
976 OSL_ENSURE( !aFntFmtId.isEmpty(), "FontFormatId not found" );
979 *pValue++ <<= aFntFmtId;
982 OSL_ENSURE( pValue - pValues == nProps, "property mismatch" );
983 PutProperties( aNames , aValues );
985 SetFormatModified( false );
989 const SmFormat & SmMathConfig::GetStandardFormat() const
991 if (!pFormat)
992 const_cast<SmMathConfig*>(this)->LoadFormat();
993 return *pFormat;
997 void SmMathConfig::SetStandardFormat( const SmFormat &rFormat, bool bSaveFontFormatList )
999 if (!pFormat)
1000 LoadFormat();
1001 if (rFormat == *pFormat)
1002 return;
1004 *pFormat = rFormat;
1005 SetFormatModified( true );
1006 SaveFormat();
1008 if (bSaveFontFormatList)
1010 // needed for SmFontTypeDialog's DefaultButtonClickHdl
1011 if (pFontFormatList)
1012 pFontFormatList->SetModified( true );
1013 SaveFontFormatList();
1018 SmPrintSize SmMathConfig::GetPrintSize() const
1020 if (!pOther)
1021 const_cast<SmMathConfig*>(this)->LoadOther();
1022 return pOther->ePrintSize;
1026 void SmMathConfig::SetPrintSize( SmPrintSize eSize )
1028 if (!pOther)
1029 LoadOther();
1030 if (eSize != pOther->ePrintSize)
1032 pOther->ePrintSize = eSize;
1033 SetOtherModified( true );
1038 sal_uInt16 SmMathConfig::GetPrintZoomFactor() const
1040 if (!pOther)
1041 const_cast<SmMathConfig*>(this)->LoadOther();
1042 return pOther->nPrintZoomFactor;
1046 void SmMathConfig::SetPrintZoomFactor( sal_uInt16 nVal )
1048 if (!pOther)
1049 LoadOther();
1050 if (nVal != pOther->nPrintZoomFactor)
1052 pOther->nPrintZoomFactor = nVal;
1053 SetOtherModified( true );
1058 void SmMathConfig::SetOtherIfNotEqual( bool &rbItem, bool bNewVal )
1060 if (bNewVal != rbItem)
1062 rbItem = bNewVal;
1063 SetOtherModified( true );
1068 bool SmMathConfig::IsPrintTitle() const
1070 if (!pOther)
1071 const_cast<SmMathConfig*>(this)->LoadOther();
1072 return pOther->bPrintTitle;
1076 void SmMathConfig::SetPrintTitle( bool bVal )
1078 if (!pOther)
1079 LoadOther();
1080 SetOtherIfNotEqual( pOther->bPrintTitle, bVal );
1084 bool SmMathConfig::IsPrintFormulaText() const
1086 if (!pOther)
1087 const_cast<SmMathConfig*>(this)->LoadOther();
1088 return pOther->bPrintFormulaText;
1092 void SmMathConfig::SetPrintFormulaText( bool bVal )
1094 if (!pOther)
1095 LoadOther();
1096 SetOtherIfNotEqual( pOther->bPrintFormulaText, bVal );
1099 bool SmMathConfig::IsSaveOnlyUsedSymbols() const
1101 if (!pOther)
1102 const_cast<SmMathConfig*>(this)->LoadOther();
1103 return pOther->bIsSaveOnlyUsedSymbols;
1106 bool SmMathConfig::IsAutoCloseBrackets() const
1108 if (!pOther)
1109 const_cast<SmMathConfig*>(this)->LoadOther();
1110 return pOther->bIsAutoCloseBrackets;
1113 bool SmMathConfig::IsPrintFrame() const
1115 if (!pOther)
1116 const_cast<SmMathConfig*>(this)->LoadOther();
1117 return pOther->bPrintFrame;
1121 void SmMathConfig::SetPrintFrame( bool bVal )
1123 if (!pOther)
1124 LoadOther();
1125 SetOtherIfNotEqual( pOther->bPrintFrame, bVal );
1129 void SmMathConfig::SetSaveOnlyUsedSymbols( bool bVal )
1131 if (!pOther)
1132 LoadOther();
1133 SetOtherIfNotEqual( pOther->bIsSaveOnlyUsedSymbols, bVal );
1137 void SmMathConfig::SetAutoCloseBrackets( bool bVal )
1139 if (!pOther)
1140 LoadOther();
1141 SetOtherIfNotEqual( pOther->bIsAutoCloseBrackets, bVal );
1145 bool SmMathConfig::IsIgnoreSpacesRight() const
1147 if (!pOther)
1148 const_cast<SmMathConfig*>(this)->LoadOther();
1149 return pOther->bIgnoreSpacesRight;
1153 void SmMathConfig::SetIgnoreSpacesRight( bool bVal )
1155 if (!pOther)
1156 LoadOther();
1157 SetOtherIfNotEqual( pOther->bIgnoreSpacesRight, bVal );
1161 bool SmMathConfig::IsAutoRedraw() const
1163 if (!pOther)
1164 const_cast<SmMathConfig*>(this)->LoadOther();
1165 return pOther->bAutoRedraw;
1169 void SmMathConfig::SetAutoRedraw( bool bVal )
1171 if (!pOther)
1172 LoadOther();
1173 SetOtherIfNotEqual( pOther->bAutoRedraw, bVal );
1177 bool SmMathConfig::IsShowFormulaCursor() const
1179 if (!pOther)
1180 const_cast<SmMathConfig*>(this)->LoadOther();
1181 return pOther->bFormulaCursor;
1185 void SmMathConfig::SetShowFormulaCursor( bool bVal )
1187 if (!pOther)
1188 LoadOther();
1189 SetOtherIfNotEqual( pOther->bFormulaCursor, bVal );
1192 void SmMathConfig::Notify( const css::uno::Sequence< OUString >& )
1196 void SmMathConfig::ItemSetToConfig(const SfxItemSet &rSet)
1198 const SfxPoolItem *pItem = nullptr;
1200 sal_uInt16 nU16;
1201 bool bVal;
1202 if (rSet.GetItemState(SID_PRINTSIZE, true, &pItem) == SfxItemState::SET)
1203 { nU16 = static_cast<const SfxUInt16Item *>(pItem)->GetValue();
1204 SetPrintSize( static_cast<SmPrintSize>(nU16) );
1206 if (rSet.GetItemState(SID_PRINTZOOM, true, &pItem) == SfxItemState::SET)
1207 { nU16 = static_cast<const SfxUInt16Item *>(pItem)->GetValue();
1208 SetPrintZoomFactor( nU16 );
1210 if (rSet.GetItemState(SID_PRINTTITLE, true, &pItem) == SfxItemState::SET)
1211 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1212 SetPrintTitle( bVal );
1214 if (rSet.GetItemState(SID_PRINTTEXT, true, &pItem) == SfxItemState::SET)
1215 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1216 SetPrintFormulaText( bVal );
1218 if (rSet.GetItemState(SID_PRINTFRAME, true, &pItem) == SfxItemState::SET)
1219 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1220 SetPrintFrame( bVal );
1222 if (rSet.GetItemState(SID_AUTOREDRAW, true, &pItem) == SfxItemState::SET)
1223 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1224 SetAutoRedraw( bVal );
1226 if (rSet.GetItemState(SID_NO_RIGHT_SPACES, true, &pItem) == SfxItemState::SET)
1227 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1228 if (IsIgnoreSpacesRight() != bVal)
1230 SetIgnoreSpacesRight( bVal );
1232 // reformat (displayed) formulas accordingly
1233 Broadcast(SfxHint(SfxHintId::MathFormatChanged));
1236 if (rSet.GetItemState(SID_SAVE_ONLY_USED_SYMBOLS, true, &pItem) == SfxItemState::SET)
1237 { bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1238 SetSaveOnlyUsedSymbols( bVal );
1241 if (rSet.GetItemState(SID_AUTO_CLOSE_BRACKETS, true, &pItem) == SfxItemState::SET)
1243 bVal = static_cast<const SfxBoolItem *>(pItem)->GetValue();
1244 SetAutoCloseBrackets( bVal );
1247 SaveOther();
1251 void SmMathConfig::ConfigToItemSet(SfxItemSet &rSet) const
1253 const SfxItemPool *pPool = rSet.GetPool();
1255 rSet.Put(SfxUInt16Item(pPool->GetWhich(SID_PRINTSIZE),
1256 sal::static_int_cast<sal_uInt16>(GetPrintSize())));
1257 rSet.Put(SfxUInt16Item(pPool->GetWhich(SID_PRINTZOOM),
1258 GetPrintZoomFactor()));
1260 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_PRINTTITLE), IsPrintTitle()));
1261 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_PRINTTEXT), IsPrintFormulaText()));
1262 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_PRINTFRAME), IsPrintFrame()));
1263 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_AUTOREDRAW), IsAutoRedraw()));
1264 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_NO_RIGHT_SPACES), IsIgnoreSpacesRight()));
1265 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_SAVE_ONLY_USED_SYMBOLS), IsSaveOnlyUsedSymbols()));
1266 rSet.Put(SfxBoolItem(pPool->GetWhich(SID_AUTO_CLOSE_BRACKETS), IsAutoCloseBrackets()));
1269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */