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) 2026 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 "headerfooterlayout.h"
24 :
25 : #include "dom/factory.h"
26 : #include "dom/masterscore.h"
27 : #include "dom/mscore.h"
28 : #include "dom/page.h"
29 : #include "dom/text.h"
30 :
31 : #include "ifileinfoprovider.h"
32 : #include "tlayout.h"
33 :
34 : using namespace mu::engraving;
35 : using namespace mu::engraving::rendering::score;
36 :
37 4581 : void HeaderFooterLayout::layoutHeaderFooter(LayoutContext& ctx, Page* page)
38 : {
39 4581 : const bool isPageMode = ctx.conf().isMode(LayoutMode::PAGE) || ctx.conf().isMode(LayoutMode::FLOAT);
40 4581 : if (!isPageMode) {
41 0 : return;
42 : }
43 :
44 4581 : const int n = page->getDisplayPageNumber();
45 :
46 4581 : if (mustShowHeadersForPage(ctx, page)) {
47 147 : const bool odd = (n & 1) || !ctx.conf().styleB(Sid::headerOddEven);
48 147 : createUpdateHeaderText(ctx, page, 0, ctx.conf().styleSt(odd ? Sid::oddHeaderL : Sid::evenHeaderL));
49 147 : createUpdateHeaderText(ctx, page, 1, ctx.conf().styleSt(odd ? Sid::oddHeaderC : Sid::evenHeaderC));
50 147 : createUpdateHeaderText(ctx, page, 2, ctx.conf().styleSt(odd ? Sid::oddHeaderR : Sid::evenHeaderR));
51 : } else {
52 17736 : for (int area = 0; area < MAX_HEADERS; ++area) {
53 13302 : removeHeaderText(page, area);
54 : }
55 : }
56 :
57 4581 : if (mustShowFootersForPage(ctx, page)) {
58 4576 : const bool odd = (n & 1) || !ctx.conf().styleB(Sid::footerOddEven);
59 4576 : createUpdateFooterText(ctx, page, 0, ctx.conf().styleSt(odd ? Sid::oddFooterL : Sid::evenFooterL));
60 4576 : createUpdateFooterText(ctx, page, 1, ctx.conf().styleSt(odd ? Sid::oddFooterC : Sid::evenFooterC));
61 4576 : createUpdateFooterText(ctx, page, 2, ctx.conf().styleSt(odd ? Sid::oddFooterR : Sid::evenFooterR));
62 : } else {
63 20 : for (int area = 0; area < MAX_FOOTERS; ++area) {
64 15 : removeFooterText(page, area);
65 : }
66 : }
67 : }
68 :
69 441 : void HeaderFooterLayout::createUpdateHeaderText(LayoutContext& ctx, Page* page, int area, const String& s)
70 : {
71 441 : if (s.empty()) {
72 270 : removeHeaderText(page, area);
73 270 : return;
74 : }
75 :
76 171 : Text* text = page->headerText(area);
77 171 : if (!text) {
78 140 : text = Factory::createText(page, TextStyleType::HEADER);
79 140 : text->setFlag(ElementFlag::MOVABLE, false);
80 140 : text->setFlag(ElementFlag::GENERATED, true); // set to disable editing
81 140 : text->setLayoutToParentWidth(true);
82 140 : page->setHeaderText(area, text);
83 :
84 140 : switch (area) {
85 91 : case 0:
86 91 : text->setAlign({ AlignH::LEFT, AlignV::TOP });
87 91 : text->setPosition(AlignH::LEFT);
88 91 : break;
89 6 : case 1:
90 6 : text->setAlign({ AlignH::HCENTER, AlignV::TOP });
91 6 : text->setPosition(AlignH::HCENTER);
92 6 : break;
93 43 : case 2:
94 43 : text->setAlign({ AlignH::RIGHT, AlignV::TOP });
95 43 : text->setPosition(AlignH::RIGHT);
96 43 : break;
97 0 : default:
98 0 : break;
99 : }
100 : }
101 :
102 171 : if (!updateHeaderFooterText(ctx, page, text, s)) {
103 19 : removeHeaderText(page, area);
104 : }
105 : }
106 :
107 13728 : void HeaderFooterLayout::createUpdateFooterText(LayoutContext& ctx, Page* page, int area, const String& s)
108 : {
109 13728 : if (s.empty()) {
110 8095 : removeFooterText(page, area);
111 8095 : return;
112 : }
113 :
114 5633 : Text* text = page->footerText(area);
115 5633 : if (!text) {
116 5633 : text = Factory::createText(page, TextStyleType::FOOTER);
117 5633 : text->setFlag(ElementFlag::MOVABLE, false);
118 5633 : text->setFlag(ElementFlag::GENERATED, true); // set to disable editing
119 5633 : text->setLayoutToParentWidth(true);
120 5633 : page->setFooterText(area, text);
121 :
122 5633 : switch (area) {
123 32 : case 0:
124 32 : text->setAlign({ AlignH::LEFT, AlignV::BOTTOM });
125 32 : text->setPosition(AlignH::LEFT);
126 32 : break;
127 4576 : case 1:
128 4576 : text->setAlign({ AlignH::HCENTER, AlignV::BOTTOM });
129 4576 : text->setPosition(AlignH::HCENTER);
130 4576 : break;
131 1025 : case 2:
132 1025 : text->setAlign({ AlignH::RIGHT, AlignV::BOTTOM });
133 1025 : text->setPosition(AlignH::RIGHT);
134 1025 : break;
135 0 : default:
136 0 : break;
137 : }
138 : }
139 :
140 5633 : if (!updateHeaderFooterText(ctx, page, text, s)) {
141 5539 : removeFooterText(page, area);
142 : }
143 : }
144 :
145 5804 : bool HeaderFooterLayout::updateHeaderFooterText(LayoutContext& ctx, Page* page, Text* text, const String& s)
146 : {
147 : // Hack: we can't use toXmlEscaped on the entire string because this would erase any manual XML
148 : // formatting, but we do want to be able to use a plain '&' in favour of XML character entities ...
149 : // ... also, opening less-than characters are escaped if they are followed by a space or a digit,
150 : // to avoid confusion with XML tags, but not if they are followed by a letter
151 5804 : String escaped;
152 32797 : for (size_t i = 0, n = s.size(); i < n; ++i) {
153 26993 : const Char c = s.at(i);
154 26993 : if (c == '&') {
155 0 : escaped += u"&";
156 0 : continue;
157 26993 : } else if (c == '<' && (i + 1 < n) && (s.at(i + 1).isSpace() || s.at(i + 1).isDigit())) {
158 0 : escaped += u"<";
159 0 : continue;
160 : }
161 26993 : escaped += c;
162 : }
163 :
164 : // first formatting pass - apply TextStyleType formatting and any manual XML formatting
165 5804 : text->setXmlText(escaped);
166 5804 : text->createBlocks();
167 :
168 : // second formatting pass - replace macros and apply their unique formatting (if any)
169 5804 : int emptyBlocks = 0;
170 5804 : std::vector<TextBlock> newBlocks;
171 11644 : for (const TextBlock& oldBlock : text->ldata()->blocks) {
172 5840 : Text* dummyText = Factory::createText(ctx.dom().dummyParent(), text->textStyleType());
173 11680 : dummyText->mutldata()->blocks = { replaceTextMacros(ctx, page, oldBlock) };
174 5840 : dummyText->genText();
175 5840 : dummyText->createBlocks();
176 5840 : if (dummyText->xmlText().isEmpty()) {
177 5594 : emptyBlocks++; // count empty blocks to remove them later if there is only 1 empty block
178 : }
179 5840 : newBlocks.insert(newBlocks.end(), dummyText->ldata()->blocks.cbegin(), dummyText->ldata()->blocks.cend());
180 5840 : delete dummyText;
181 : }
182 :
183 5804 : if (newBlocks.empty() || (newBlocks.size() == 1 && emptyBlocks == 1)) {
184 : // If there is no block, or the only block is empty, tell the caller that the text should be removed.
185 : // This way, empty lines are preserved if and only if there also is some text anywhere in the header/footer.
186 5558 : return false;
187 : }
188 :
189 246 : text->mutldata()->blocks = newBlocks;
190 246 : TLayout::layoutText(text, text->mutldata());
191 :
192 246 : return true;
193 11644 : }
194 :
195 13591 : void HeaderFooterLayout::removeHeaderText(Page* page, int area)
196 : {
197 13591 : if (Text* t = page->headerText(area)) {
198 19 : page->setHeaderText(area, nullptr);
199 19 : delete t;
200 : }
201 13591 : }
202 :
203 13649 : void HeaderFooterLayout::removeFooterText(Page* page, int area)
204 : {
205 13649 : if (Text* t = page->footerText(area)) {
206 5539 : page->setFooterText(area, nullptr);
207 5539 : delete t;
208 : }
209 13649 : }
210 :
211 : //---------------------------------------------------------
212 : // replaceTextMacros
213 : // (keep in sync with toolTipHeaderFooter in EditStyle::EditStyle())
214 : // $p - page number, except on first page
215 : // $P - page number, on all pages
216 : // $N - page number, if there is more than one
217 : // $n - number of pages
218 : // $i - part name, except on first page
219 : // $I - part name, on all pages
220 : // $f - file name
221 : // $F - file path+name
222 : // $d - current date
223 : // $D - creation date
224 : // $m - last modification time
225 : // $M - last modification date
226 : // $C - copyright, on first page only
227 : // $c - copyright, on all pages
228 : // $v - MuseScore version the score was last saved with
229 : // $r - MuseScore revision the score was last saved with
230 : // $$ - the $ sign itself
231 : // $:tag: - any metadata tag
232 : //
233 : // tags always defined (see Score::init())):
234 : // copyright
235 : // creationDate
236 : // movementNumber
237 : // movementTitle
238 : // platform
239 : // source
240 : // workNumber
241 : // workTitle
242 : //---------------------------------------------------------
243 :
244 5840 : TextBlock HeaderFooterLayout::replaceTextMacros(LayoutContext& ctx, const Page* page, const TextBlock& tb)
245 : {
246 5840 : std::list<TextFragment> newFragments;
247 11680 : for (const TextFragment& tf: tb.fragments()) {
248 5840 : const CharFormat defaultFormat = tf.format;
249 5840 : const String& s = tf.text;
250 :
251 : // if this is the first fragment, or the current fragment has a different format, we need to start a new fragment
252 5840 : if (newFragments.size() == 0 || newFragments.back().format != defaultFormat) {
253 5840 : newFragments.emplace_back(TextFragment());
254 5840 : newFragments.back().format = defaultFormat;
255 : }
256 :
257 11930 : for (size_t i = 0, n = s.size(); i < n; ++i) {
258 6090 : Char c = s.at(i);
259 6090 : if (c == '$' && (i < (n - 1))) {
260 5802 : Char nc = s.at(i + 1);
261 5802 : switch (nc.toAscii()) {
262 1197 : case 'p': // not on first page of the project nor page number 1
263 1197 : if (!page->pageNumber() || page->getDisplayPageNumber() <= 1) {
264 1031 : break;
265 : }
266 : [[fallthrough]];
267 : case 'N': // on page 1 only if there are multiple pages
268 182 : if ((static_cast<int>(page->score()->npages()) + page->score()->pageNumberOffset()) <= 1) {
269 6 : ctx.mutState().setMustRecomputeHeadersFooters(true);
270 6 : break;
271 : }
272 : [[fallthrough]];
273 : case 'P': // on all pages
274 : {
275 189 : const int no = page->getDisplayPageNumber();
276 189 : if (no > 0) {
277 189 : const String pageNumberString = String::number(no);
278 189 : const CharFormat pageNumberFormat = formatForMacro(ctx, String('$' + nc));
279 189 : appendFormattedString(newFragments, pageNumberString, defaultFormat, pageNumberFormat);
280 189 : }
281 : }
282 189 : break;
283 0 : case 'n':
284 : {
285 0 : ctx.mutState().setMustRecomputeHeadersFooters(true);
286 0 : const int no = static_cast<int>(page->score()->npages()) + page->score()->pageNumberOffset();
287 0 : const String numberOfPagesString = String::number(no);
288 0 : const CharFormat pageNumberFormat = formatForMacro(ctx, String('$' + nc));
289 0 : appendFormattedString(newFragments, numberOfPagesString, defaultFormat, pageNumberFormat);
290 0 : }
291 0 : break;
292 0 : case 'i': // not on first page
293 0 : if (!page->pageNumber()) {
294 0 : break;
295 : }
296 : [[fallthrough]];
297 : case 'I':
298 0 : newFragments.back().text += page->score()->metaTag(u"partName");
299 0 : break;
300 0 : case 'f':
301 0 : newFragments.back().text += page->score()->masterScore()->fileInfo()->fileName(false).toString();
302 0 : break;
303 0 : case 'F':
304 0 : newFragments.back().text += page->score()->masterScore()->fileInfo()->path().toString();
305 0 : break;
306 0 : case 'd':
307 0 : newFragments.back().text += muse::Date::currentDate().toString(muse::DateFormat::ISODate);
308 0 : break;
309 0 : case 'D':
310 : {
311 0 : String creationDate = page->score()->metaTag(u"creationDate");
312 0 : if (creationDate.isEmpty()) {
313 0 : newFragments.back().text += page->score()->masterScore()->fileInfo()->birthTime().date().toString(
314 0 : muse::DateFormat::ISODate);
315 : } else {
316 0 : newFragments.back().text += muse::Date::fromStringISOFormat(creationDate).toString(
317 0 : muse::DateFormat::ISODate);
318 : }
319 0 : }
320 0 : break;
321 0 : case 'm': {
322 0 : IFileInfoProviderPtr fileInfo = page->score()->masterScore()->fileInfo();
323 0 : if (fileInfo->isNewlyCreated()) {
324 0 : newFragments.back().text += String(u"HH:mm:ss");
325 : } else {
326 0 : newFragments.back().text += fileInfo->lastModified().time().toString(muse::DateFormat::ISODate);
327 : }
328 0 : }
329 0 : break;
330 0 : case 'M': {
331 0 : IFileInfoProviderPtr fileInfo = page->score()->masterScore()->fileInfo();
332 0 : if (fileInfo->isNewlyCreated()) {
333 0 : newFragments.back().text += String(u"YYYY-MM-DD");
334 : } else {
335 0 : newFragments.back().text += fileInfo->lastModified().date().toString(muse::DateFormat::ISODate);
336 : }
337 0 : }
338 0 : break;
339 3455 : case 'C': // only on first page
340 3455 : if (page->pageNumber()) {
341 130 : break;
342 : }
343 : [[fallthrough]];
344 : case 'c':
345 : {
346 3325 : const String copyrightString = page->score()->metaTag(u"copyright");
347 3325 : const CharFormat copyrightFormat = formatForMacro(ctx, String('$' + nc));
348 3325 : appendFormattedString(newFragments, copyrightString, defaultFormat, copyrightFormat);
349 3325 : }
350 3325 : break;
351 0 : case 'v':
352 0 : if (page->score()->dirty()) {
353 0 : newFragments.back().text += application()->version().toString();
354 : } else {
355 0 : newFragments.back().text += page->score()->mscoreVersion();
356 : }
357 0 : break;
358 0 : case 'r':
359 0 : if (page->score()->dirty()) {
360 0 : newFragments.back().text += application()->revision();
361 : } else {
362 0 : int rev = page->score()->mscoreRevision();
363 0 : if (rev > 0 && rev <= 5709) { // MuseScore 1.x and earlier used decimal numbers, referring to a commit on SourceForge.net, the largest being 5709
364 0 : newFragments.back().text += String::number(rev, 10);
365 0 : } else if (rev > 0xffffff) { // MuseScore 2.0 and later use a >= 7-digit hex SHA, referring to a commit on GitHub.com
366 0 : newFragments.back().text += String::number(rev, 16);
367 : } else { // unknown, like in a self-built development version, or in a very old version of MuseScore before the revision number was tracked
368 0 : newFragments.back().text += String::number(rev, 10); // or "" or "Unknown"?
369 : }
370 : }
371 0 : break;
372 0 : case '$':
373 0 : newFragments.back().text += '$';
374 0 : break;
375 1121 : case ':':
376 : {
377 1121 : String tag;
378 1121 : size_t k = i + 2;
379 11210 : for (; k < n; ++k) {
380 11210 : if (s.at(k) == u':') {
381 1121 : break;
382 : }
383 10089 : tag += s.at(k);
384 : }
385 1121 : if (k != n) { // found ':' ?
386 1121 : if (tag == u"copyright") {
387 1121 : const String copyrightString = page->score()->metaTag(tag);
388 1121 : const CharFormat copyrightFormat = formatForMacro(ctx, String(u"$:" + tag + u":"));
389 1121 : appendFormattedString(newFragments, copyrightString, defaultFormat, copyrightFormat);
390 1121 : } else {
391 0 : newFragments.back().text += page->score()->metaTag(tag);
392 : }
393 1121 : i = k - 1;
394 : }
395 1121 : }
396 1121 : break;
397 0 : default:
398 0 : newFragments.back().text += '$';
399 0 : newFragments.back().text += nc;
400 0 : break;
401 : }
402 5802 : ++i;
403 : } else {
404 288 : newFragments.back().text += c;
405 : }
406 : }
407 5840 : }
408 :
409 5840 : TextBlock newBlock;
410 5840 : newBlock.fragments() = newFragments;
411 5840 : return newBlock;
412 5840 : }
413 :
414 4635 : CharFormat HeaderFooterLayout::formatForMacro(const LayoutContext& ctx, const String& macro)
415 : {
416 4635 : CharFormat format;
417 4635 : if (macro == "$c" || macro == "$C" || macro == "$:copyright:") {
418 4446 : format.setStyle(ctx.conf().styleV(Sid::copyrightFontStyle).value<FontStyle>());
419 4446 : format.setFontSize(ctx.conf().styleD(Sid::copyrightFontSize));
420 4446 : format.setFontFamily(ctx.conf().styleSt(Sid::copyrightFontFace));
421 189 : } else if (macro == "$p" || macro == "$P" || macro == "$n" || macro == "$N") {
422 189 : format.setStyle(ctx.conf().styleV(Sid::pageNumberFontStyle).value<FontStyle>());
423 189 : format.setFontSize(ctx.conf().styleD(Sid::pageNumberFontSize));
424 189 : format.setFontFamily(ctx.conf().styleSt(Sid::pageNumberFontFace));
425 : }
426 4635 : return format;
427 0 : }
428 :
429 4635 : void HeaderFooterLayout::appendFormattedString(std::list<TextFragment>& fragments, const String& string, const CharFormat& defaultFormat,
430 : const CharFormat& newFormat)
431 : {
432 : // If the default format equals the format for this macro, we don't need to create a new fragment...
433 4635 : if (defaultFormat == newFormat) {
434 4482 : fragments.back().text += string;
435 4482 : return;
436 : }
437 153 : TextFragment newFragment(string);
438 153 : newFragment.format = newFormat;
439 153 : fragments.emplace_back(newFragment);
440 153 : fragments.emplace_back(TextFragment()); // Start next fragment
441 153 : fragments.back().format = defaultFormat; // reset to default for next fragment
442 153 : }
443 :
444 4575 : double HeaderFooterLayout::headerExtension(const LayoutContext& ctx, const Page* page)
445 : {
446 4575 : const bool isPageMode = ctx.conf().isMode(LayoutMode::PAGE) || ctx.conf().isMode(LayoutMode::FLOAT);
447 4575 : if (!isPageMode) {
448 0 : return 0.0;
449 : }
450 :
451 4575 : if (mustShowHeadersForPage(ctx, page)) {
452 141 : double maxHeight = 0.0;
453 564 : for (int area = 0; area < MAX_HEADERS; ++area) {
454 423 : if (Text* text = page->headerText(area)) {
455 140 : double h = text->height();
456 140 : if (h > maxHeight) {
457 136 : maxHeight = h;
458 : }
459 : }
460 : }
461 141 : const double offset = ctx.conf().styleV(Sid::headerOffset).value<PointF>().y() * DPMM;
462 141 : return std::max(0.0, maxHeight - offset);
463 : }
464 :
465 4434 : return 0.0;
466 : }
467 :
468 4575 : double HeaderFooterLayout::footerExtension(const LayoutContext& ctx, const Page* page)
469 : {
470 4575 : const bool isPageMode = ctx.conf().isMode(LayoutMode::PAGE) || ctx.conf().isMode(LayoutMode::FLOAT);
471 4575 : if (!isPageMode) {
472 0 : return 0.0;
473 : }
474 :
475 4575 : if (mustShowFootersForPage(ctx, page)) {
476 4570 : double maxHeight = 0.0;
477 18280 : for (int area = 0; area < MAX_FOOTERS; ++area) {
478 13710 : if (Text* text = page->footerText(area)) {
479 94 : double h = text->height();
480 94 : if (h > maxHeight) {
481 86 : maxHeight = h;
482 : }
483 : }
484 : }
485 4570 : const double offset = ctx.conf().styleV(Sid::footerOffset).value<PointF>().y() * DPMM;
486 4570 : return std::max(0.0, maxHeight - offset);
487 : }
488 5 : return 0.0;
489 : }
490 :
491 0 : bool HeaderFooterLayout::containsTimestampMacros(const String& text)
492 : {
493 0 : return text.contains(u"$d")
494 0 : || text.contains(u"$D")
495 0 : || text.contains(u"$m")
496 0 : || text.contains(u"$M");
497 : }
498 :
499 0 : bool HeaderFooterLayout::scoreHasTimestampHeadersFooters(const Score* score)
500 : {
501 0 : const MStyle& style = score->style();
502 :
503 0 : if (style.styleB(Sid::showHeader)) {
504 0 : if (containsTimestampMacros(style.styleSt(Sid::oddHeaderL))
505 0 : || containsTimestampMacros(style.styleSt(Sid::oddHeaderC))
506 0 : || containsTimestampMacros(style.styleSt(Sid::oddHeaderR))
507 0 : || containsTimestampMacros(style.styleSt(Sid::evenHeaderL))
508 0 : || containsTimestampMacros(style.styleSt(Sid::evenHeaderC))
509 0 : || containsTimestampMacros(style.styleSt(Sid::evenHeaderR))) {
510 0 : return true;
511 : }
512 : }
513 :
514 0 : if (style.styleB(Sid::showFooter)) {
515 0 : if (containsTimestampMacros(style.styleSt(Sid::oddFooterL))
516 0 : || containsTimestampMacros(style.styleSt(Sid::oddFooterC))
517 0 : || containsTimestampMacros(style.styleSt(Sid::oddFooterR))
518 0 : || containsTimestampMacros(style.styleSt(Sid::evenFooterL))
519 0 : || containsTimestampMacros(style.styleSt(Sid::evenFooterC))
520 0 : || containsTimestampMacros(style.styleSt(Sid::evenFooterR))) {
521 0 : return true;
522 : }
523 : }
524 :
525 0 : return false;
526 : }
527 :
528 9156 : bool HeaderFooterLayout::mustShowHeadersForPage(const LayoutContext& ctx, const Page* page)
529 : {
530 9156 : return ctx.conf().styleB(Sid::showHeader)
531 15976 : && ((page->pageNumber() && page->getDisplayPageNumber() != 1)
532 15976 : || ctx.conf().styleB(Sid::headerFirstPage));
533 : }
534 :
535 9156 : bool HeaderFooterLayout::mustShowFootersForPage(const LayoutContext& ctx, const Page* page)
536 : {
537 9156 : return ctx.conf().styleB(Sid::showFooter)
538 17968 : && ((page->pageNumber() && page->getDisplayPageNumber() != 1)
539 17968 : || ctx.conf().styleB(Sid::footerFirstPage));
540 : }
|