LCOV - code coverage report
Current view: top level - src/engraving/style - defaultstyle.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 83.1 % 65 54
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "defaultstyle.h"
      23              : 
      24              : #include "io/file.h"
      25              : 
      26              : #include "log.h"
      27              : 
      28              : using namespace mu;
      29              : using namespace muse::io;
      30              : using namespace mu::engraving;
      31              : 
      32              : static const int LEGACY_MSC_VERSION_V450 = 450;
      33              : static const int LEGACY_MSC_VERSION_V302 = 302;
      34              : static const int LEGACY_MSC_VERSION_V3 = 301;
      35              : static const int LEGACY_MSC_VERSION_V2 = 206;
      36              : static const int LEGACY_MSC_VERSION_V1 = 114;
      37              : 
      38              : static const String LEGACY_MSS_V1_PATH(u":/engraving/styles/legacy-style-defaults-v1.mss");
      39              : static const String LEGACY_MSS_V2_PATH(u":/engraving/styles/legacy-style-defaults-v2.mss");
      40              : static const String LEGACY_MSS_V3_PATH(u":/engraving/styles/legacy-style-defaults-v3.mss");
      41              : static const String LEGACY_MSS_V302_PATH(u":/engraving/styles/legacy-style-defaults-v302.mss");
      42              : static const String LEGACY_MSS_V450_PATH(u":/engraving/styles/legacy-style-defaults-v450.mss");
      43              : 
      44      1712207 : DefaultStyle* DefaultStyle::instance()
      45              : {
      46      1712207 :     static DefaultStyle s;
      47      1712207 :     return &s;
      48              : }
      49              : 
      50           16 : static void applyPageSizeToStyle(MStyle* style, const SizeF& pageSize)
      51              : {
      52           16 :     double oldWidth = style->styleD(Sid::pageWidth);
      53           16 :     double newPrintableWidth = style->styleD(Sid::pagePrintableWidth) + (pageSize.width() - oldWidth);
      54              : 
      55           16 :     style->set(Sid::pageWidth, pageSize.width());
      56           16 :     style->set(Sid::pageHeight, pageSize.height());
      57           16 :     style->set(Sid::pagePrintableWidth, newPrintableWidth);
      58           16 : }
      59              : 
      60           16 : void DefaultStyle::init(const path_t& defaultStyleFilePath, const path_t& partStyleFilePath, const SizeF& defaultPageSize)
      61              : {
      62           16 :     m_baseStyle.precomputeValues();
      63              : 
      64              :     {
      65           16 :         applyPageSizeToStyle(&m_defaultStyle, defaultPageSize);
      66              : 
      67           16 :         if (!defaultStyleFilePath.empty()) {
      68            0 :             bool ok = doLoadStyle(&m_defaultStyle, defaultStyleFilePath);
      69            0 :             if (!ok) {
      70            0 :                 LOGW() << "Failed to load default style file from " << defaultStyleFilePath;
      71              :             }
      72              :         }
      73              : 
      74           16 :         m_defaultStyle.precomputeValues();
      75              :     }
      76              : 
      77           16 :     if (!partStyleFilePath.empty()) {
      78            0 :         m_defaultStyleForParts = new MStyle();
      79              : 
      80            0 :         applyPageSizeToStyle(m_defaultStyleForParts, defaultPageSize);
      81              : 
      82            0 :         bool ok = doLoadStyle(m_defaultStyleForParts, partStyleFilePath);
      83            0 :         if (!ok) {
      84            0 :             LOGW() << "Failed to load default part style file from " << partStyleFilePath;
      85            0 :             delete m_defaultStyleForParts;
      86            0 :             m_defaultStyleForParts = nullptr;
      87              :         } else {
      88            0 :             m_defaultStyleForParts->precomputeValues();
      89              :         }
      90              :     }
      91           16 : }
      92              : 
      93           16 : bool DefaultStyle::doLoadStyle(MStyle* style, const path_t& filePath)
      94              : {
      95           16 :     File file(filePath);
      96           16 :     if (!file.open(IODevice::ReadOnly)) {
      97            5 :         LOGE() << "failed load style: " << filePath;
      98            5 :         return false;
      99              :     }
     100              : 
     101           11 :     return style->read(&file);
     102           16 : }
     103              : 
     104              : // Static
     105              : 
     106      1692200 : const MStyle& DefaultStyle::baseStyle()
     107              : {
     108      1692200 :     return instance()->m_baseStyle;
     109              : }
     110              : 
     111        19826 : const MStyle& DefaultStyle::defaultStyle()
     112              : {
     113        19826 :     return instance()->m_defaultStyle;
     114              : }
     115              : 
     116          165 : const MStyle* DefaultStyle::defaultStyleForParts()
     117              : {
     118          165 :     return instance()->m_defaultStyleForParts;
     119              : }
     120              : 
     121      2250680 : const MStyle& DefaultStyle::resolveStyleDefaults(const int defaultsVersion)
     122              : {
     123       565092 :     static auto loadedStyle = [](MStyle& style, const String& path, bool& loaded_flag) -> const MStyle&
     124              :     {
     125       565092 :         if (loaded_flag) {
     126       565076 :             return style;
     127              :         }
     128           16 :         loaded_flag = doLoadStyle(&style, path);
     129           16 :         return style;
     130              :     };
     131              : 
     132      2250680 :     switch (defaultsVersion) {
     133            5 :     case LEGACY_MSC_VERSION_V450: {
     134            5 :         static MStyle style_v450;
     135              :         static bool loaded_v450 = false;
     136            5 :         return loadedStyle(style_v450, LEGACY_MSS_V450_PATH, loaded_v450);
     137              :     } break;
     138        20537 :     case LEGACY_MSC_VERSION_V302: {
     139        20537 :         static MStyle style_v302;
     140              :         static bool loaded_v302 = false;
     141        20537 :         return loadedStyle(style_v302, LEGACY_MSS_V302_PATH, loaded_v302);
     142              :     } break;
     143       427469 :     case LEGACY_MSC_VERSION_V3: {
     144       427469 :         static MStyle style_v3;
     145              :         static bool loaded_v3 = false;
     146       427469 :         return loadedStyle(style_v3, LEGACY_MSS_V3_PATH, loaded_v3);
     147              :     } break;
     148        63677 :     case LEGACY_MSC_VERSION_V2: {
     149        63677 :         static MStyle style_v2;
     150              :         static bool loaded_v2 = false;
     151        63677 :         return loadedStyle(style_v2, LEGACY_MSS_V2_PATH, loaded_v2);
     152              :     } break;
     153        53404 :     case LEGACY_MSC_VERSION_V1: {
     154        53404 :         static MStyle style_v1;
     155              :         static bool loaded_v1 = false;
     156        53404 :         return loadedStyle(style_v1, LEGACY_MSS_V1_PATH, loaded_v1);
     157              :     } break;
     158      1685588 :     default:
     159      1685588 :         return baseStyle();
     160              :     }
     161              : }
        

Generated by: LCOV version 2.0-1