22
22
#include " notationmeta.h"
23
23
24
24
#include < cmath>
25
+ #include < climits>
25
26
26
27
#include < QJsonArray>
27
28
#include < QJsonObject>
@@ -41,6 +42,76 @@ static QString boolToString(bool b)
41
42
return b ? " true" : " false" ;
42
43
}
43
44
45
+ static bool shouldTryRecognizeText (const Text* text)
46
+ {
47
+ const TextStyleType type = text->textStyleType ();
48
+ if (type == TextStyleType::DEFAULT || type == TextStyleType::FRAME) {
49
+ return true ;
50
+ }
51
+
52
+ return (int )(type) >= (int )TextStyleType::USER1 && (int )(type) <= (int )TextStyleType::USER12;
53
+ }
54
+
55
+ static QString recognizeTitle (const mu::engraving::Score* score)
56
+ {
57
+ const MeasureBase* mb = score->first ();
58
+ if (!mb || !mb->isVBox ()) {
59
+ return QString ();
60
+ }
61
+
62
+ const Text* titleText = nullptr ;
63
+ double maxFontSize = score->style ().value (Sid::titleFontSize).toDouble ();
64
+ double minY = DBL_MAX;
65
+
66
+ for (const EngravingItem* item : mb->el ()) {
67
+ if (!item || !item->isText ()) {
68
+ continue ;
69
+ }
70
+
71
+ const Text* text = toText (item);
72
+ if (!shouldTryRecognizeText (text)) {
73
+ continue ;
74
+ }
75
+
76
+ if (RealIsEqualOrMore (text->size (), maxFontSize) && RealIsEqualOrLess (text->y (), minY)) {
77
+ titleText = text;
78
+ maxFontSize = text->size ();
79
+ minY = text->y ();
80
+ }
81
+ }
82
+
83
+ return titleText ? titleText->plainText ().toQString () : QString ();
84
+ }
85
+
86
+ static QString recognizeComposer (const mu::engraving::Score* score)
87
+ {
88
+ const MeasureBase* mb = score->first ();
89
+ if (!mb || !mb->isVBox ()) {
90
+ return QString ();
91
+ }
92
+
93
+ const Text* rightmostText = nullptr ;
94
+ double rightmostTextX = mb->ldata ()->bbox ().center ().x ();
95
+
96
+ for (const EngravingItem* item : mb->el ()) {
97
+ if (!item || !item->isText ()) {
98
+ continue ;
99
+ }
100
+
101
+ const Text* text = toText (item);
102
+ if (!shouldTryRecognizeText (text)) {
103
+ continue ;
104
+ }
105
+
106
+ if (text->x () > rightmostTextX) {
107
+ rightmostText = text;
108
+ rightmostTextX = text->x ();
109
+ }
110
+ }
111
+
112
+ return rightmostText ? rightmostText->plainText ().toQString () : QString ();
113
+ }
114
+
44
115
RetVal<std::string> NotationMeta::metaJson (notation::INotationPtr notation)
45
116
{
46
117
IF_ASSERT_FAILED (notation) {
@@ -98,6 +169,10 @@ QString NotationMeta::title(const mu::engraving::Score* score)
98
169
title = score->metaTag (u" workTitle" );
99
170
}
100
171
172
+ if (title.isEmpty ()) {
173
+ title = recognizeTitle (score);
174
+ }
175
+
101
176
if (title.isEmpty ()) {
102
177
title = score->name ();
103
178
}
@@ -128,6 +203,10 @@ QString NotationMeta::composer(const mu::engraving::Score* score)
128
203
composer = score->metaTag (u" composer" );
129
204
}
130
205
206
+ if (composer.isEmpty ()) {
207
+ composer = recognizeComposer (score);
208
+ }
209
+
131
210
return composer;
132
211
}
133
212
0 commit comments