MUSX Document Model
Loading...
Searching...
No Matches
Document.h
1/*
2 * Copyright (C) 2024, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <memory>
25#include <cstdint>
26#include <filesystem>
27#include <optional>
28#include <string>
29#include <unordered_map>
30#include <vector>
31
32#include "Header.h"
33#include "Instrument.h"
34#include "ObjectPool.h"
35#include "MusxInstance.h"
36
41namespace musx {
42
43namespace factory {
44class DocumentFactory;
45} // namespace factory
46
51namespace dom {
52
53using namespace header;
54
55enum class KnownShapeDefType;
56using EmbeddedGraphicBlob = std::vector<uint8_t>;
57
64
65using EmbeddedGraphicsMap = std::unordered_map<Cmper, EmbeddedGraphicData>;
66
71{
72 Ignore,
74
75 Apply
82};
83
88{
89public:
91 [[nodiscard]]
92 HeaderPtr& getHeader() { return m_header; }
94 [[nodiscard]]
95 const HeaderPtr& getHeader() const { return m_header; }
96
98 [[nodiscard]]
99 OptionsPoolPtr& getOptions() { return m_options; }
101 [[nodiscard]]
102 const OptionsPoolPtr& getOptions() const { return m_options; }
103
105 [[nodiscard]]
106 OthersPoolPtr& getOthers() { return m_others; }
108 [[nodiscard]]
109 const OthersPoolPtr& getOthers() const { return m_others; }
110
112 [[nodiscard]]
113 DetailsPoolPtr& getDetails() { return m_details; }
115 [[nodiscard]]
116 const DetailsPoolPtr& getDetails() const { return m_details; }
117
119 [[nodiscard]]
120 EntryPoolPtr& getEntries() { return m_entries; }
122 [[nodiscard]]
123 const EntryPoolPtr& getEntries() const { return m_entries; }
124
126 [[nodiscard]]
127 TextsPoolPtr& getTexts() { return m_texts; }
129 [[nodiscard]]
130 const TextsPoolPtr& getTexts() const { return m_texts; }
131
133 [[nodiscard]]
134 PartVoicingPolicy getPartVoicingPolicy() const { return m_partVoicingPolicy; }
135
137 [[nodiscard]]
138 std::optional<double> getScoreDurationSeconds() const { return m_scoreDurationSeconds; }
139
141 [[nodiscard]]
142 const EmbeddedGraphicsMap& getEmbeddedGraphics() const { return m_embeddedGraphics; }
143
146 [[nodiscard]]
147 constexpr Cmper calcScrollViewCmper([[maybe_unused]]Cmper partId) const noexcept
148 {
149 // as of now, there is no calculation, but keep that assumption encapsulated.
150 return BASE_SYSTEM_ID;
151 }
152
155 [[nodiscard]]
157
161 [[nodiscard]]
163
165 [[nodiscard]]
166 std::optional<KnownShapeDefType> getCachedShapeRecognition(Cmper shapeCmper) const;
167
169 void setCachedShapeRecognition(Cmper shapeCmper, KnownShapeDefType type) const;
170
174 [[nodiscard]]
176
180 [[nodiscard]]
182
184 [[nodiscard]]
185 int getMaxBlankPages() const { return m_maxBlankPages; }
186
188 [[nodiscard]]
190 {
191 MUSX_ASSERT_IF(!m_instruments.has_value()) {
192 throw std::logic_error("Attempted to retrieve instrument map before it was constructed.");
193 }
194 return m_instruments.value();
195 }
196
198 [[nodiscard]]
199 const std::optional<std::filesystem::path>& getSourcePath() const { return m_sourcePath; }
200
204 [[nodiscard]]
205 std::optional<std::filesystem::path> resolveExternalGraphicPath(Cmper fileDescId) const;
206
209 [[nodiscard]]
210 const InstrumentInfo& getInstrumentForStaff(StaffCmper staffId) const;
211
216 [[nodiscard]]
217 std::optional<InstrumentInfo> getInstrumentForStaff(Cmper partId, StaffCmper staffId) const;
218
232 [[nodiscard]]
233 InstrumentMap createInstrumentMap(Cmper forPartId) const;
234
237 [[nodiscard]]
238 bool calcHasVaryingSystemStaves(Cmper forPartId) const;
239
241 [[nodiscard]]
243
252 [[nodiscard]]
253 std::vector<MeasCmper> calcJumpFromMeasures(Cmper partId, MeasCmper currentMeasure) const;
254
259 bool iterateEntries(Cmper partId, std::function<bool(const EntryInfoPtr&)> iterator) const;
260
261private:
263 explicit Document() = default;
264
265 DocumentWeakPtr m_self;
266
267 HeaderPtr m_header;
268 OptionsPoolPtr m_options;
269 OthersPoolPtr m_others;
270 DetailsPoolPtr m_details;
271 EntryPoolPtr m_entries;
272 TextsPoolPtr m_texts;
273
274 int m_maxBlankPages{};
275
276 std::optional<InstrumentMap> m_instruments = std::nullopt;
279
280 PartVoicingPolicy m_partVoicingPolicy{};
281 std::optional<double> m_scoreDurationSeconds;
282 EmbeddedGraphicsMap m_embeddedGraphics;
283 std::optional<std::filesystem::path> m_sourcePath;
284
285 mutable std::unordered_map<Cmper, KnownShapeDefType> m_shapeRecognitionCache;
286
287 // Grant the factory class access to the private constructor
289};
290
291} // namespace dom
292} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:88
std::optional< double > getScoreDurationSeconds() const
Returns the score playback duration in seconds, if provided by NotationMetadata.xml.
Definition Document.h:138
bool iterateEntries(Cmper partId, std::function< bool(const EntryInfoPtr &)> iterator) const
Iterate all entries in the document by staff and then measure. This function wraps MusxInstanceList<o...
Definition Document.cpp:429
const TextsPoolPtr & getTexts() const
Retrieves the const texts pool.
Definition Document.h:130
OthersPoolPtr & getOthers()
Retrieves the others pool.
Definition Document.h:106
MusxInstance< others::Page > calcPageFromMeasure(Cmper partId, MeasCmper measureId) const
Searches pages to find the page that contains the measure.
Definition Document.cpp:163
std::optional< KnownShapeDefType > getCachedShapeRecognition(Cmper shapeCmper) const
Retrieves a cached shape recognition result, if available.
Definition Document.cpp:149
const HeaderPtr & getHeader() const
Retrieves the const header.
Definition Document.h:95
MusxInstance< others::StaffSystem > calcSystemFromMeasure(Cmper partId, MeasCmper measureId) const
Searches systems to find the page that contains the measure.
Definition Document.cpp:181
const InstrumentInfo & getInstrumentForStaff(StaffCmper staffId) const
Get the instrument info for the given staffId.
Definition Document.cpp:311
int getMaxBlankPages() const
Returns the maximum number of blank pages in any part. This is calculated by factory::DocumentFactory...
Definition Document.h:185
PartVoicingPolicy getPartVoicingPolicy() const
Retrieves the document's part voicing policy.
Definition Document.h:134
const InstrumentMap & getInstruments() const
Returns the instrument map for this document. It is computed by the factory.
Definition Document.h:189
std::optional< std::filesystem::path > resolveExternalGraphicPath(Cmper fileDescId) const
Resolve an external graphic path using SCORE_PARTID.
Definition Document.cpp:438
MusxInstanceList< others::StaffUsed > getStudioViewStaves(Cmper partId) const
Returns the Studio View staves for the given partId. Normally this is only one staff and is invariant...
Definition Document.cpp:138
const std::optional< std::filesystem::path > & getSourcePath() const
Returns the path to the musx (or EnigmaXML) file used to create this document, if provided.
Definition Document.h:199
std::vector< MeasCmper > calcJumpFromMeasures(Cmper partId, MeasCmper currentMeasure) const
Calculates the measures from which playback jumped into currentMeasure.
Definition Document.cpp:359
constexpr Cmper calcScrollViewCmper(Cmper partId) const noexcept
Returns the Scroll View Cmper for the given partId.
Definition Document.h:147
const DetailsPoolPtr & getDetails() const
Retrieves the const others pool.
Definition Document.h:116
HeaderPtr & getHeader()
Retrieves the header.
Definition Document.h:92
MusxInstanceList< others::StaffUsed > getScrollViewStaves(Cmper partId) const
Returns the Scroll View staves for the given partId.
Definition Document.cpp:133
bool calcHasVaryingSystemStaves(Cmper forPartId) const
Calculate if the current score/part has staves that differ from system to system.
Definition Document.cpp:341
const OptionsPoolPtr & getOptions() const
Retrieves the const options pool.
Definition Document.h:102
const EmbeddedGraphicsMap & getEmbeddedGraphics() const
Returns all embedded graphics keyed by graphic cmper.
Definition Document.h:142
TextsPoolPtr & getTexts()
Retrieves the texts pool.
Definition Document.h:127
void setCachedShapeRecognition(Cmper shapeCmper, KnownShapeDefType type) const
Stores a shape recognition result in the cache.
Definition Document.cpp:158
const OthersPoolPtr & getOthers() const
Retrieves the const others pool.
Definition Document.h:109
InstrumentMap createInstrumentMap(Cmper forPartId) const
Builds an instrument map for the specified linked part ID.
Definition Document.cpp:198
MusicRange calcEntireDocument() const
Calcuate a MusicRange instance for the entire document.
Definition Document.cpp:335
const EntryPoolPtr & getEntries() const
Retrieves the entry others pool.
Definition Document.h:123
DetailsPoolPtr & getDetails()
Retrieves the details pool.
Definition Document.h:113
OptionsPoolPtr & getOptions()
Retrieves the options pool.
Definition Document.h:99
EntryPoolPtr & getEntries()
Retrieves the entry pool.
Definition Document.h:120
Wraps a frame of shared_ptr<const EntryInfo> and an index for per entry access. This class manages ow...
Definition Entries.h:549
Derived description of a logical instrument in a document.
Definition Instrument.h:48
A list of instruments, which may be single- or multi-staff.
Definition Instrument.h:107
Utility class that represents of a range of musical time.
Definition CommonClasses.h:492
Provides optional per-type extension methods for MusxInstanceList.
Definition MusxInstance.h:118
Factory class for creating Document objects from XML.
Definition DocumentFactory.h:49
std::shared_ptr< Header > HeaderPtr
Shared Header pointer.
Definition Header.h:106
KnownShapeDefType
Enumerates the shape types we can recognize semantically.
Definition ShapeDesigner.h:39
std::shared_ptr< OthersPool > OthersPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:500
int16_t MeasCmper
Enigma meas Cmper (may be negative when not applicable)
Definition Fundamentals.h:64
std::shared_ptr< const T > MusxInstance
Defines the type of a musx instance stored in a pool.
Definition MusxInstance.h:40
std::shared_ptr< DetailsPool > DetailsPoolPtr
Shared DetailsPool pointer.
Definition ObjectPool.h:587
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
std::shared_ptr< TextsPool > TextsPoolPtr
Shared OthersPool pointer.
Definition ObjectPool.h:661
PartVoicingPolicy
Controls whether Finale-style part voicing is applied when iterating entries via musx::dom::details::...
Definition Document.h:71
std::shared_ptr< OptionsPool > OptionsPoolPtr
Shared OptionsPool pointer.
Definition ObjectPool.h:446
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition DocumentElement.h:37
std::vector< uint8_t > EmbeddedGraphicBlob
Raw bytes for one embedded graphic payload from a musx archive.
Definition Document.h:56
std::unordered_map< Cmper, EmbeddedGraphicData > EmbeddedGraphicsMap
Embedded graphics keyed by cmper (filename stem in musx archive).
Definition Document.h:65
constexpr Cmper BASE_SYSTEM_ID
Definition Fundamentals.h:92
int16_t StaffCmper
Enigma staff (staffId) Cmper (may be negative when not applicable)
Definition Fundamentals.h:65
std::shared_ptr< EntryPool > EntryPoolPtr
Shared EntryPool pointer.
Definition ObjectPool.h:622
object model for musx file (enigmaxml)
Definition BaseClasses.h:38
Embedded graphic payload from a musx archive entry.
Definition Document.h:60
EmbeddedGraphicBlob bytes
Raw file bytes.
Definition Document.h:62
std::string extension
File extension without the leading dot (e.g. "png", "jpg", "svg").
Definition Document.h:61