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 "ObjectPool.h"
34#include "MusxInstance.h"
35
40namespace musx {
41
42namespace factory {
43class DocumentFactory;
44} // namespace factory
45
50namespace dom {
51
52using namespace header;
53
54struct InstrumentInfo;
55enum class KnownShapeDefType;
56using InstrumentMap = std::unordered_map<StaffCmper, InstrumentInfo>;
57using EmbeddedGraphicBlob = std::vector<uint8_t>;
58
65
66using EmbeddedGraphicsMap = std::unordered_map<Cmper, EmbeddedGraphicData>;
71{
72 std::unordered_map<StaffCmper, size_t> staves;
75
77 std::vector<StaffCmper> getSequentialStaves() const;
78
83 static const InstrumentInfo* getInstrumentForStaff(const InstrumentMap& map, StaffCmper staffId);
84};
85
90{
91 Ignore,
93
94 Apply
101};
102
107{
108public:
109
111 [[nodiscard]]
112 HeaderPtr& getHeader() { return m_header; }
114 [[nodiscard]]
115 const HeaderPtr& getHeader() const { return m_header; }
116
118 [[nodiscard]]
119 OptionsPoolPtr& getOptions() { return m_options; }
121 [[nodiscard]]
122 const OptionsPoolPtr& getOptions() const { return m_options; }
123
125 [[nodiscard]]
126 OthersPoolPtr& getOthers() { return m_others; }
128 [[nodiscard]]
129 const OthersPoolPtr& getOthers() const { return m_others; }
130
132 [[nodiscard]]
133 DetailsPoolPtr& getDetails() { return m_details; }
135 [[nodiscard]]
136 const DetailsPoolPtr& getDetails() const { return m_details; }
137
139 [[nodiscard]]
140 EntryPoolPtr& getEntries() { return m_entries; }
142 [[nodiscard]]
143 const EntryPoolPtr& getEntries() const { return m_entries; }
144
146 [[nodiscard]]
147 TextsPoolPtr& getTexts() { return m_texts; }
149 [[nodiscard]]
150 const TextsPoolPtr& getTexts() const { return m_texts; }
151
153 [[nodiscard]]
154 PartVoicingPolicy getPartVoicingPolicy() const { return m_partVoicingPolicy; }
155
157 [[nodiscard]]
158 std::optional<double> getScoreDurationSeconds() const { return m_scoreDurationSeconds; }
159
161 [[nodiscard]]
162 const EmbeddedGraphicsMap& getEmbeddedGraphics() const { return m_embeddedGraphics; }
163
166 [[nodiscard]]
167 constexpr Cmper calcScrollViewCmper([[maybe_unused]]Cmper partId) const noexcept
168 {
169 // as of now, there is no calculation, but keep that assumption encapsulated.
170 return BASE_SYSTEM_ID;
171 }
172
175 [[nodiscard]]
177
181 [[nodiscard]]
183
185 [[nodiscard]]
186 std::optional<KnownShapeDefType> getCachedShapeRecognition(Cmper shapeCmper) const;
187
189 void setCachedShapeRecognition(Cmper shapeCmper, KnownShapeDefType type) const;
190
194 [[nodiscard]]
196
200 [[nodiscard]]
202
204 [[nodiscard]]
205 int getMaxBlankPages() const { return m_maxBlankPages; }
206
208 [[nodiscard]]
209 const InstrumentMap& getInstruments() const { return m_instruments; }
210
212 [[nodiscard]]
213 const std::optional<std::filesystem::path>& getSourcePath() const { return m_sourcePath; }
214
218 [[nodiscard]]
219 std::optional<std::filesystem::path> resolveExternalGraphicPath(Cmper fileDescId) const;
220
223 [[nodiscard]]
224 const InstrumentInfo& getInstrumentForStaff(StaffCmper staffId) const;
225
239 [[nodiscard]]
240 InstrumentMap createInstrumentMap(Cmper forPartId) const;
241
244 [[nodiscard]]
245 bool calcHasVaryingSystemStaves(Cmper forPartId) const;
246
248 [[nodiscard]]
250
259 [[nodiscard]]
260 std::vector<MeasCmper> calcJumpFromMeasures(Cmper partId, MeasCmper currentMeasure) const;
261
266 bool iterateEntries(Cmper partId, std::function<bool(const EntryInfoPtr&)> iterator) const;
267
268private:
270 explicit Document() = default;
271
272 DocumentWeakPtr m_self;
273
274 HeaderPtr m_header;
275 OptionsPoolPtr m_options;
276 OthersPoolPtr m_others;
277 DetailsPoolPtr m_details;
278 EntryPoolPtr m_entries;
279 TextsPoolPtr m_texts;
280
281 int m_maxBlankPages{};
282
283 InstrumentMap m_instruments;
285
286 PartVoicingPolicy m_partVoicingPolicy{};
287 std::optional<double> m_scoreDurationSeconds;
288 EmbeddedGraphicsMap m_embeddedGraphics;
289 std::optional<std::filesystem::path> m_sourcePath;
290
291 mutable std::unordered_map<Cmper, KnownShapeDefType> m_shapeRecognitionCache;
292
293 // Grant the factory class access to the private constructor
295};
296
297} // namespace dom
298} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:107
std::optional< double > getScoreDurationSeconds() const
Returns the score playback duration in seconds, if provided by NotationMetadata.xml.
Definition Document.h:158
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:415
const TextsPoolPtr & getTexts() const
Retrieves the const texts pool.
Definition Document.h:150
OthersPoolPtr & getOthers()
Retrieves the others pool.
Definition Document.h:126
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:115
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:205
PartVoicingPolicy getPartVoicingPolicy() const
Retrieves the document's part voicing policy.
Definition Document.h:154
const InstrumentMap & getInstruments() const
Returns the instrument map for this document. It is computed by the factory.
Definition Document.h:209
std::optional< std::filesystem::path > resolveExternalGraphicPath(Cmper fileDescId) const
Resolve an external graphic path using SCORE_PARTID.
Definition Document.cpp:424
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:213
std::vector< MeasCmper > calcJumpFromMeasures(Cmper partId, MeasCmper currentMeasure) const
Calculates the measures from which playback jumped into currentMeasure.
Definition Document.cpp:345
constexpr Cmper calcScrollViewCmper(Cmper partId) const noexcept
Returns the Scroll View Cmper for the given partId.
Definition Document.h:167
const DetailsPoolPtr & getDetails() const
Retrieves the const others pool.
Definition Document.h:136
HeaderPtr & getHeader()
Retrieves the header.
Definition Document.h:112
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:327
const OptionsPoolPtr & getOptions() const
Retrieves the const options pool.
Definition Document.h:122
const EmbeddedGraphicsMap & getEmbeddedGraphics() const
Returns all embedded graphics keyed by graphic cmper.
Definition Document.h:162
TextsPoolPtr & getTexts()
Retrieves the texts pool.
Definition Document.h:147
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:129
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:321
const EntryPoolPtr & getEntries() const
Retrieves the entry others pool.
Definition Document.h:143
DetailsPoolPtr & getDetails()
Retrieves the details pool.
Definition Document.h:133
OptionsPoolPtr & getOptions()
Retrieves the options pool.
Definition Document.h:119
EntryPoolPtr & getEntries()
Retrieves the entry pool.
Definition Document.h:140
Wraps a frame of shared_ptr<const EntryInfo> and an index for per entry access. This class manages ow...
Definition Entries.h:512
Utility class that represents of a range of musical time.
Definition CommonClasses.h:422
Provides optional per-type extension methods for MusxInstanceList.
Definition MusxInstance.h:103
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:39
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:90
std::shared_ptr< OptionsPool > OptionsPoolPtr
Shared OptionsPool pointer.
Definition ObjectPool.h:446
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition BaseClasses.h:57
std::vector< uint8_t > EmbeddedGraphicBlob
Raw bytes for one embedded graphic payload from a musx archive.
Definition Document.h:57
std::unordered_map< Cmper, EmbeddedGraphicData > EmbeddedGraphicsMap
Definition Document.h:66
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
std::unordered_map< StaffCmper, InstrumentInfo > InstrumentMap
A list of instruments, which may be single- or multi-staff.
Definition Document.h:56
object model for musx file (enigmaxml)
Definition BaseClasses.h:36
Embedded graphic payload from a musx archive entry.
Definition Document.h:61
EmbeddedGraphicBlob bytes
Raw file bytes.
Definition Document.h:63
std::string extension
File extension without the leading dot (e.g. "png", "jpg", "svg").
Definition Document.h:62
Represents information about each instrument in the document. This is calculated from the staves,...
Definition Document.h:71
std::vector< StaffCmper > getSequentialStaves() const
Returns the staffIds in sequence as they appear in Scroll View in the score.
Definition Document.cpp:503
Cmper multistaffGroupId
The others::MultiStaffInstrumentGroup that defines the instrument. (May be zero.)
Definition Document.h:74
static const InstrumentInfo * getInstrumentForStaff(const InstrumentMap &map, StaffCmper staffId)
Get the instrument info for the given staffId in the given map.
Definition Document.cpp:517
Cmper staffGroupId
The details::StaffGroup that visually represents the instrument. (May be zero.)
Definition Document.h:73
std::unordered_map< StaffCmper, size_t > staves
List of each staffId with its sequence index from top to bottom.
Definition Document.h:72