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 :
23 : #pragma once
24 :
25 : #include <vector>
26 : #include <array>
27 :
28 : #include "../types/types.h"
29 :
30 : namespace mu::engraving {
31 : class Score;
32 :
33 : enum class AccidentalVal : signed char;
34 :
35 : //---------------------------------------------------------
36 : // KeySym
37 : // position of one symbol in KeySig
38 : //---------------------------------------------------------
39 :
40 : struct KeySym {
41 : SymId sym = SymId::noSym;
42 : int line = 0; // relative line position (first staffline: line == 0, first gap: line == 1, ...)
43 : Spatium xPos = 0.0_sp; // x position in staff spatium units
44 : };
45 :
46 : //---------------------------------------------------------
47 : // CustDef
48 : // definition of one symbol in Custom KeySig
49 : //---------------------------------------------------------
50 :
51 : struct CustDef {
52 : int degree = 0; // scale degree
53 : SymId sym = SymId::noSym;
54 : Spatium xAlt = 0.0_sp; // x position alteration in spatium units (default symbol position is based on index)
55 : int octAlt = 0; // octave alteration
56 : };
57 :
58 : //---------------------------------------------------------
59 : // KeySigEvent
60 : //---------------------------------------------------------
61 :
62 : class KeySigEvent
63 : {
64 : public:
65 7255 : KeySigEvent() = default;
66 :
67 : bool operator==(const KeySigEvent& e) const;
68 : bool operator!=(const KeySigEvent& e) const { return !(*this == e); }
69 :
70 : void setConcertKey(Key v);
71 : void setKey(Key v);
72 : void setCustom(bool val);
73 : void print() const;
74 :
75 14165 : Key concertKey() const { return m_concertKey; }
76 1646288 : Key key() const { return m_key; }
77 1998 : KeyMode mode() const { return m_mode; }
78 383 : void setMode(KeyMode m) { m_mode = m; }
79 84493 : bool custom() const { return m_custom; }
80 1023 : bool isValid() const { return m_key != Key::INVALID; }
81 1936 : bool isAtonal() const { return m_mode == KeyMode::NONE; }
82 5 : void setForInstrumentChange(bool forInstrumentChange) { m_forInstrumentChange = forInstrumentChange; }
83 1389 : bool forInstrumentChange() const { return m_forInstrumentChange; }
84 : void initFromSubtype(int); // for backward compatibility
85 : int degInKey(int degree) const; // return "absolute degree"
86 : SymId symInKey(SymId sym, int degree) const;
87 45 : std::vector<CustDef>& customKeyDefs() { return m_customKeyDefs; }
88 152 : const std::vector<CustDef>& customKeyDefs() const { return m_customKeyDefs; }
89 :
90 : private:
91 :
92 : void enforceLimits(bool transposing = false); // if true, enforce only trnasposing, otherways both
93 :
94 : Key m_concertKey = Key::INVALID; // -7 -> +7
95 : Key m_key = Key::INVALID; // actual key, depends on staff transposition
96 : KeyMode m_mode = KeyMode::UNKNOWN;
97 : bool m_custom = false;
98 : bool m_forInstrumentChange = false;
99 : std::vector<CustDef> m_customKeyDefs;
100 : };
101 :
102 : //---------------------------------------------------------
103 : // AccidentalState
104 : /// Contains a state for every absolute staff line.
105 : //---------------------------------------------------------
106 :
107 : static const int TIE_CONTEXT = 0x10;
108 : static const int MIN_ACC_STATE = 0;
109 : static const int MAX_ACC_STATE = 75;
110 :
111 : class AccidentalState
112 : {
113 : public:
114 46746 : AccidentalState() {}
115 : void init(Key key);
116 : void init(const KeySigEvent&);
117 : AccidentalVal accidentalVal(int line, bool& error) const;
118 : AccidentalVal accidentalVal(int line) const;
119 : bool forceRestateAccidental(int line) const;
120 : bool tieContext(int line) const;
121 : void setAccidentalVal(int line, AccidentalVal val, bool tieContext = false);
122 : void setForceRestateAccidental(int line, bool forceRestate);
123 :
124 : private:
125 :
126 : uint8_t m_state[MAX_ACC_STATE] = {}; // (0 -- 4) | TIE_CONTEXT
127 : std::array<bool, MAX_ACC_STATE> m_forceRestateAccidental;
128 : };
129 :
130 : struct Interval;
131 :
132 : enum class PreferSharpFlat : char;
133 : extern Interval calculateInterval(Key key1, Key key2);
134 : } // namespace mu::engraving
|