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 : #include "symbol.h"
24 :
25 : #include "score.h"
26 :
27 : #include "draw/fontmetrics.h"
28 : #include "iengravingfont.h"
29 :
30 : #include "types/symnames.h"
31 :
32 : #include "image.h"
33 : #include "staff.h"
34 :
35 : #include "log.h"
36 :
37 : using namespace mu;
38 : using namespace muse::draw;
39 : using namespace mu::engraving;
40 :
41 : namespace mu::engraving {
42 : //---------------------------------------------------------
43 : // Symbol
44 : //---------------------------------------------------------
45 :
46 1391 : Symbol::Symbol(const ElementType& type, EngravingObject* parent, ElementFlags f)
47 1391 : : BSymbol(type, parent, f)
48 : {
49 1391 : m_sym = SymId::accidentalSharp; // arbitrary valid default
50 1391 : }
51 :
52 108 : Symbol::Symbol(EngravingObject* parent, ElementFlags f)
53 108 : : Symbol(ElementType::SYMBOL, parent, f)
54 : {
55 108 : }
56 :
57 432 : Symbol::Symbol(const Symbol& s)
58 432 : : BSymbol(s)
59 : {
60 432 : m_sym = s.m_sym;
61 432 : m_scoreFont = s.m_scoreFont;
62 432 : m_symbolsSize = s.m_symbolsSize;
63 432 : m_symAngle = s.m_symAngle;
64 432 : }
65 :
66 3697 : void Symbol::setSym(SymId s, const std::shared_ptr<IEngravingFont>& sf)
67 : {
68 3697 : m_sym = s;
69 :
70 3697 : if (sf.get()) {
71 18 : m_scoreFont = sf;
72 : }
73 3697 : }
74 :
75 : //---------------------------------------------------------
76 : // symName
77 : //---------------------------------------------------------
78 :
79 0 : AsciiStringView Symbol::symName() const
80 : {
81 0 : return SymNames::nameForSymId(m_sym);
82 : }
83 :
84 : //---------------------------------------------------------
85 : // accessibleInfo
86 : //---------------------------------------------------------
87 :
88 0 : String Symbol::accessibleInfo() const
89 : {
90 0 : return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), SymNames::userNameForSymId(m_sym).translated());
91 : }
92 :
93 : //---------------------------------------------------------
94 : // subtypeUserName
95 : //---------------------------------------------------------
96 :
97 0 : muse::TranslatableString Symbol::subtypeUserName() const
98 : {
99 0 : return SymNames::userNameForSymId(m_sym);
100 : }
101 :
102 : //---------------------------------------------------------
103 : // Symbol::getProperty
104 : //---------------------------------------------------------
105 4371 : PropertyValue Symbol::getProperty(Pid propertyId) const
106 : {
107 4371 : switch (propertyId) {
108 0 : case Pid::SYMBOL:
109 0 : return PropertyValue::fromValue(m_sym);
110 0 : case Pid::SCORE_FONT:
111 0 : if (m_scoreFont) {
112 0 : return PropertyValue::fromValue(String::fromStdString(m_scoreFont->name()));
113 : } else {
114 0 : return PropertyValue::fromValue(String());
115 : }
116 0 : case Pid::SYMBOLS_SIZE:
117 0 : return PropertyValue::fromValue(m_symbolsSize);
118 2 : case Pid::SYMBOL_ANGLE:
119 2 : return PropertyValue::fromValue(m_symAngle);
120 4369 : default:
121 4369 : break;
122 : }
123 4369 : return BSymbol::getProperty(propertyId);
124 : }
125 :
126 : //---------------------------------------------------------
127 : // Symbol::setProperty
128 : //---------------------------------------------------------
129 :
130 71 : bool Symbol::setProperty(Pid propertyId, const PropertyValue& v)
131 : {
132 71 : switch (propertyId) {
133 0 : case Pid::SYMBOL:
134 0 : m_sym = v.value<SymId>();
135 0 : break;
136 14 : case Pid::SCORE_FONT:
137 14 : m_scoreFont = score()->engravingFonts()->fontByName(v.value<String>().toStdString());
138 14 : break;
139 14 : case Pid::SYMBOLS_SIZE:
140 14 : m_symbolsSize = v.toDouble();
141 14 : break;
142 0 : case Pid::SYMBOL_ANGLE:
143 0 : m_symAngle = v.toDouble();
144 0 : break;
145 43 : default:
146 43 : break;
147 : }
148 71 : triggerLayout();
149 71 : return BSymbol::setProperty(propertyId, v);
150 : }
151 :
152 : //---------------------------------------------------------
153 : // propertyDefault
154 : //---------------------------------------------------------
155 :
156 4287 : PropertyValue Symbol::propertyDefault(Pid propertyId) const
157 : {
158 4287 : switch (propertyId) {
159 0 : case Pid::SYMBOL:
160 0 : return SymId::accidentalSharp;
161 2 : case Pid::SYMBOL_ANGLE:
162 2 : return 0.0;
163 0 : case Pid::SYMBOLS_SIZE:
164 0 : return 1.0;
165 0 : case Pid::SCORE_FONT:
166 0 : if (m_scoreFont) {
167 0 : return style().styleSt(Sid::musicalSymbolFont);
168 : } else {
169 0 : return String();
170 : }
171 4285 : default:
172 4285 : break;
173 : }
174 4285 : return EngravingItem::propertyDefault(propertyId);
175 : }
176 :
177 0 : void Symbol::reset()
178 : {
179 0 : undoResetProperty(Pid::SYMBOLS_SIZE);
180 0 : undoResetProperty(Pid::SYMBOL_ANGLE);
181 0 : undoResetProperty(Pid::SCORE_FONT);
182 0 : BSymbol::reset();
183 0 : }
184 :
185 : //---------------------------------------------------------
186 : // FSymbol
187 : //---------------------------------------------------------
188 :
189 0 : FSymbol::FSymbol(DummyParentOr<EngravingItem> parent)
190 0 : : BSymbol(ElementType::FSYMBOL, parent)
191 : {
192 0 : m_code = 0;
193 0 : m_font.setNoFontMerging(true);
194 0 : }
195 :
196 0 : FSymbol::FSymbol(const FSymbol& s)
197 0 : : BSymbol(s)
198 : {
199 0 : m_font = s.m_font;
200 0 : m_code = s.m_code;
201 0 : }
202 :
203 : //---------------------------------------------------------
204 : // toString
205 : // FSymbol is a single code point but code points above 2^16 cannot be
206 : // represented by a single Char, hence we return a String instead. Char
207 : // and String use the UTF-16 encoding internally (like QChar, QString).
208 : //---------------------------------------------------------
209 :
210 0 : String FSymbol::toString() const
211 : {
212 0 : return String::fromUcs4(m_code);
213 : }
214 :
215 : //---------------------------------------------------------
216 : // accessibleInfo
217 : // Screen readers know how to pronounce the common font symbols so we can
218 : // return just the character itself. Similarly, common characters should
219 : // be rendered correctly by Braille terminals.
220 : //---------------------------------------------------------
221 :
222 0 : String FSymbol::accessibleInfo() const
223 : {
224 0 : return toString();
225 : }
226 :
227 : //---------------------------------------------------------
228 : // setFont
229 : //---------------------------------------------------------
230 :
231 0 : void FSymbol::setFont(const Font& f)
232 : {
233 0 : m_font = f;
234 0 : m_font.setNoFontMerging(true);
235 0 : }
236 : }
|