Skip to content

Commit

Permalink
added score write hook
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkorsukov authored and vpereverzev committed Jul 22, 2021
1 parent 7328101 commit 641a5bc
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 73 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ set(YOUTUBE_API_KEY "" CACHE STRING "YouTube API key")

option(USE_SCORE_ACCESSIBLE_TREE "Use score accessible tree" OFF)

# Temporary flags for MU3 compatibility to make testing easier.
option(ENGRAVING_COMPAT_WRITESTYLE_302 "Write style to score xml file" ON)
# -----

option(SOUNDFONT3 "Ogg Vorbis compressed fonts" ON) # Enable Ogg Vorbis compressed fonts, requires Ogg & Vorbis
option(DOWNLOAD_SOUNDFONT "Download the latest soundfont version as part of the build process" ON)

Expand Down
1 change: 1 addition & 0 deletions build/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#cmakedefine BUILD_VST

#cmakedefine USE_SCORE_ACCESSIBLE_TREE
#cmakedefine ENGRAVING_COMPAT_WRITESTYLE_302

#cmakedefine WIN_SPARKLE_ENABLED
#cmakedefine MAC_SPARKLE_ENABLED
Expand Down
6 changes: 4 additions & 2 deletions src/engraving/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ set(MODULE_SRC
${CMAKE_CURRENT_LIST_DIR}/compat/pageformat.h
${CMAKE_CURRENT_LIST_DIR}/compat/chordlist.cpp
${CMAKE_CURRENT_LIST_DIR}/compat/chordlist.h
${CMAKE_CURRENT_LIST_DIR}/compat/readscore.cpp
${CMAKE_CURRENT_LIST_DIR}/compat/readscore.h
${CMAKE_CURRENT_LIST_DIR}/compat/readstyle.cpp
${CMAKE_CURRENT_LIST_DIR}/compat/readstyle.h
${CMAKE_CURRENT_LIST_DIR}/compat/readscorehook.cpp
${CMAKE_CURRENT_LIST_DIR}/compat/readscorehook.h
${CMAKE_CURRENT_LIST_DIR}/compat/writescorehook.cpp
${CMAKE_CURRENT_LIST_DIR}/compat/writescorehook.h

${CMAKE_CURRENT_LIST_DIR}/style/styledef.cpp
${CMAKE_CURRENT_LIST_DIR}/style/styledef.h
Expand Down
61 changes: 61 additions & 0 deletions src/engraving/compat/readscorehook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "readscorehook.h"
#include "readstyle.h"

#include "libmscore/score.h"
#include "libmscore/scorefont.h"

using namespace mu::engraving::compat;
using namespace Ms;

void ReadScoreHook::installReadStyleHook(Ms::MasterScore* score, const QByteArray& scoreData, const QString& completeBaseName)
{
m_readStyle = std::make_shared<ReadStyleHook>(score, scoreData, completeBaseName);
}

void ReadScoreHook::setupDefaultStyle()
{
if (!m_readStyle) {
return;
}
m_readStyle->setupDefaultStyle();
}

void ReadScoreHook::onReadStyleTag302(Ms::Score* score, Ms::XmlReader& xml)
{
if (!m_readStyle) {
return;
}

qreal sp = score->style().value(Sid::spatium).toDouble();

compat::ReadStyleHook::readStyleTag(score, xml);

// if (_layoutMode == LayoutMode::FLOAT || _layoutMode == LayoutMode::SYSTEM) {
if (score->_layoutMode == LayoutMode::FLOAT) {
// style should not change spatium in
// float mode
score->style().set(Sid::spatium, sp);
}
score->_scoreFont = ScoreFont::fontByName(score->style().value(Sid::MusicalSymbolFont).toString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "readscore.h"

using namespace mu::engraving::compat;
#ifndef MU_ENGRAVING_READSCOREHOOK_H
#define MU_ENGRAVING_READSCOREHOOK_H

#include <memory>
#include <QByteArray>
#include <QString>

namespace Ms {
class MasterScore;
class Score;
class XmlReader;
}

namespace mu::engraving::compat {
class ReadStyleHook;
class ReadScoreHook
{
public:

// Style
void installReadStyleHook(Ms::MasterScore* score, const QByteArray& scoreData, const QString& completeBaseName);
void setupDefaultStyle();
void onReadStyleTag302(Ms::Score* score, Ms::XmlReader& xml);

private:
std::shared_ptr<ReadStyleHook> m_readStyle = nullptr;
};
}

#endif // MU_ENGRAVING_READSCOREHOOK_H
3 changes: 2 additions & 1 deletion src/engraving/compat/readstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class MStyle;
}

namespace mu::engraving::compat {
struct ReadStyleHook
class ReadStyleHook
{
public:
ReadStyleHook(Ms::MasterScore* score, const QByteArray& scoreData, const QString& completeBaseName);

void setupDefaultStyle();
Expand Down
54 changes: 54 additions & 0 deletions src/engraving/compat/writescorehook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "writescorehook.h"

#include "libmscore/score.h"
#include "libmscore/xml.h"

#include "config.h"

using namespace mu::engraving::compat;

void WriteScoreHook::onWriteStyle302(Ms::Score* score, Ms::XmlWriter& xml)
{
bool isWriteStyle = false;
//! NOTE Write the style to the score file if the compatibility define is set
#ifdef ENGRAVING_COMPAT_WRITESTYLE_302
isWriteStyle = true;
#endif

//! NOTE If not the master score, because the Excerpts (parts) have not yet been write to separate files
if (!score->isMaster()) {
isWriteStyle = true;
}

//! NOTE If the test mode, because the tests have not yet been adapted to the new format
if (Ms::MScore::testMode) {
isWriteStyle = true;
}

if (score->isTopScore()) { // only top score (logic from 3.)
if (isWriteStyle) {
score->style().save(xml, true); // save only differences to buildin style (logic from 3.)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MU_ENGRAVING_WRITESCOREHOOK_H
#define MU_ENGRAVING_WRITESCOREHOOK_H

#ifndef MU_ENGRAVING_READSCORE_H
#define MU_ENGRAVING_READSCORE_H

#include <memory>
#include "readstyle.h"
namespace Ms {
class Score;
class XmlWriter;
}

namespace mu::engraving::compat {
struct ReadScoreHooks
class WriteScoreHook
{
std::shared_ptr<ReadStyleHook> readStyle = nullptr;
public:
WriteScoreHook() = default;

void onWriteStyle302(Ms::Score* score, Ms::XmlWriter& xml);
};
}

#endif // MU_ENGRAVING_READSCORE_H
#endif // MU_ENGRAVING_WRITESCOREHOOK_H
22 changes: 6 additions & 16 deletions src/engraving/libmscore/read302.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "style/defaultstyle.h"

#include "compat/chordlist.h"
#include "compat/readscore.h"
#include "compat/readscorehook.h"

#include "xml.h"
#include "score.h"
Expand All @@ -50,7 +50,7 @@ namespace Ms {
// return false on error
//---------------------------------------------------------

bool Score::read(XmlReader& e, const compat::ReadScoreHooks& hooks)
bool Score::read(XmlReader& e, compat::ReadScoreHook& hooks)
{
// HACK
// style setting compatibility settings for minor versions
Expand Down Expand Up @@ -111,17 +111,7 @@ bool Score::read(XmlReader& e, const compat::ReadScoreHooks& hooks)
} else if (tag == "markIrregularMeasures") {
_markIrregularMeasures = e.readInt();
} else if (tag == "Style") {
qreal sp = style().value(Sid::spatium).toDouble();

compat::ReadStyleHook::readStyleTag(this, e);

// if (_layoutMode == LayoutMode::FLOAT || _layoutMode == LayoutMode::SYSTEM) {
if (_layoutMode == LayoutMode::FLOAT) {
// style should not change spatium in
// float mode
style().set(Sid::spatium, sp);
}
_scoreFont = ScoreFont::fontByName(style().value(Sid::MusicalSymbolFont).toString());
hooks.onReadStyleTag302(this, e);
} else if (tag == "copyright" || tag == "rights") {
Text* text = new Text(this);
text->read(e);
Expand Down Expand Up @@ -195,7 +185,7 @@ bool Score::read(XmlReader& e, const compat::ReadScoreHooks& hooks)
ex->setPartScore(s);
e.setLastMeasure(nullptr);

compat::ReadScoreHooks hooks;
compat::ReadScoreHook hooks;
s->read(e, hooks);

s->linkMeasures(m);
Expand Down Expand Up @@ -344,7 +334,7 @@ void Score::linkMeasures(Score* score)
// read
//---------------------------------------------------------

bool MasterScore::read(XmlReader& e, const compat::ReadScoreHooks& hooks)
bool MasterScore::read(XmlReader& e, compat::ReadScoreHook& hooks)
{
if (!Score::read(e, hooks)) {
return false;
Expand Down Expand Up @@ -379,7 +369,7 @@ void MasterScore::addMovement(MasterScore* score)
// read301
//---------------------------------------------------------

Score::FileError MasterScore::read302(XmlReader& e, const mu::engraving::compat::ReadScoreHooks& hooks)
Score::FileError MasterScore::read302(XmlReader& e, mu::engraving::compat::ReadScoreHook& hooks)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
Expand Down
11 changes: 7 additions & 4 deletions src/engraving/libmscore/score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include "style/style.h"
#include "style/defaultstyle.h"

#include "compat/readscore.h"
#include "compat/readscorehook.h"
#include "compat/writescorehook.h"

#include "fermata.h"
#include "imageStore.h"
Expand Down Expand Up @@ -2123,16 +2124,18 @@ MasterScore* MasterScore::clone()
xml.header();

xml.stag("museScore version=\"" MSC_VERSION "\"");
write(xml, false);

compat::WriteScoreHook hook;
write(xml, false, hook);
xml.etag();

buffer.close();

QByteArray scoreData = buffer.buffer();
QString completeBaseName = masterScore()->fileInfo()->completeBaseName();

compat::ReadScoreHooks hooks;
hooks.readStyle = std::make_shared<compat::ReadStyleHook>(this, scoreData, completeBaseName);
compat::ReadScoreHook hooks;
hooks.installReadStyleHook(this, scoreData, completeBaseName);

XmlReader r(scoreData);
MasterScore* score = new MasterScore(style(), m_project);
Expand Down
22 changes: 13 additions & 9 deletions src/engraving/libmscore/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class EngravingProject;

namespace mu::engraving::compat {
class ScoreAccess;
struct ReadScoreHooks;
class ReadScoreHook;
class WriteScoreHook;
}

namespace mu::score {
Expand Down Expand Up @@ -458,6 +459,9 @@ class Score : public QObject, public ScoreElement
};

private:

friend class mu::engraving::compat::ReadScoreHook;

static std::set<Score*> validScores;
int _linkId { 0 };
MasterScore* _masterScore { 0 };
Expand Down Expand Up @@ -649,7 +653,7 @@ class Score : public QObject, public ScoreElement
void removeStaff(Staff*);
void addMeasure(MeasureBase*, MeasureBase*);
void readStaff(XmlReader&);
bool read(XmlReader&, const mu::engraving::compat::ReadScoreHooks& hooks);
bool read(XmlReader&, mu::engraving::compat::ReadScoreHook& hooks);
void linkMeasures(Score* score);

Excerpt* excerpt() { return _excerpt; }
Expand Down Expand Up @@ -703,10 +707,10 @@ class Score : public QObject, public ScoreElement
bool appendMeasuresFromScore(Score* score, const Fraction& startTick, const Fraction& endTick);
bool appendScore(Score*, bool addPageBreak = false, bool addSectionBreak = true);

void write(XmlWriter&, bool onlySelection);
void writeMovement(XmlWriter&, bool onlySelection);
void write(XmlWriter&, bool onlySelection, mu::engraving::compat::WriteScoreHook& hook);
void writeMovement(XmlWriter&, bool onlySelection, mu::engraving::compat::WriteScoreHook& hook);

bool writeScore(QIODevice* f, bool msczFormat, bool onlySelection = false);
bool writeScore(QIODevice* f, bool msczFormat, bool onlySelection, mu::engraving::compat::WriteScoreHook& hook);

QList<Staff*>& staves() { return _staves; }
const QList<Staff*>& staves() const { return _staves; }
Expand Down Expand Up @@ -1402,17 +1406,17 @@ class MasterScore : public Score
QFileInfo _sessionStartBackupInfo;
QFileInfo info;

bool read(XmlReader&, const mu::engraving::compat::ReadScoreHooks& hooks);
FileError read1(XmlReader&, bool ignoreVersionError, const mu::engraving::compat::ReadScoreHooks& hooks);
bool read(XmlReader&, mu::engraving::compat::ReadScoreHook& hooks);
FileError read1(XmlReader&, bool ignoreVersionError, mu::engraving::compat::ReadScoreHook& hooks);
FileError read114(XmlReader&);
FileError read206(XmlReader&);
FileError read302(XmlReader&, const mu::engraving::compat::ReadScoreHooks& hooks);
FileError read302(XmlReader&, mu::engraving::compat::ReadScoreHook& hooks);

void setPrev(MasterScore* s) { _prev = s; }
void setNext(MasterScore* s) { _next = s; }

friend class mu::engraving::compat::ScoreAccess;
friend class mu::engraving::EngravingProject;
friend class mu::engraving::compat::ScoreAccess;
MasterScore(std::shared_ptr<mu::engraving::EngravingProject> project);
MasterScore(const MStyle&, std::shared_ptr<mu::engraving::EngravingProject> project);

Expand Down
Loading

0 comments on commit 641a5bc

Please sign in to comment.