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 : #include "pageformat.h"
23 :
24 : #include "style/style.h"
25 : #include "rw/xmlreader.h"
26 :
27 : #include "dom/mscore.h"
28 :
29 : namespace mu::engraving::compat {
30 : //---------------------------------------------------------
31 : // read
32 : // <page-layout>
33 : // <page-height>
34 : // <page-width>
35 : // <landscape>1</landscape>
36 : // <page-margins type="both">
37 : // <left-margin>28.3465</left-margin>
38 : // <right-margin>28.3465</right-margin>
39 : // <top-margin>28.3465</top-margin>
40 : // <bottom-margin>56.6929</bottom-margin>
41 : // </page-margins>
42 : // </page-layout>
43 : //---------------------------------------------------------
44 :
45 27 : void PageFormat::read206(XmlReader& e)
46 : {
47 27 : double _oddRightMargin = 0.0;
48 27 : double _evenRightMargin = 0.0;
49 27 : AsciiStringView type;
50 :
51 130 : while (e.readNextStartElement()) {
52 103 : const AsciiStringView tag(e.name());
53 103 : if (tag == "page-margins") {
54 49 : type = e.asciiAttribute("type", "both");
55 49 : double lm = 0.0, rm = 0.0, tm = 0.0, bm = 0.0;
56 245 : while (e.readNextStartElement()) {
57 196 : const AsciiStringView t(e.name());
58 196 : double val = e.readDouble() * 0.5 / PPI;
59 196 : if (t == "left-margin") {
60 49 : lm = val;
61 147 : } else if (t == "right-margin") {
62 49 : rm = val;
63 98 : } else if (t == "top-margin") {
64 49 : tm = val;
65 49 : } else if (t == "bottom-margin") {
66 49 : bm = val;
67 : } else {
68 0 : e.unknown();
69 : }
70 : }
71 49 : _twosided = type == "odd" || type == "even";
72 49 : if (type == "odd" || type == "both") {
73 27 : _oddLeftMargin = lm;
74 27 : _oddRightMargin = rm;
75 27 : _oddTopMargin = tm;
76 27 : _oddBottomMargin = bm;
77 : }
78 49 : if (type == "even" || type == "both") {
79 27 : _evenLeftMargin = lm;
80 27 : _evenRightMargin = rm;
81 27 : _evenTopMargin = tm;
82 27 : _evenBottomMargin = bm;
83 : }
84 54 : } else if (tag == "page-height") {
85 27 : _size.setHeight(e.readDouble() * 0.5 / PPI);
86 27 : } else if (tag == "page-width") {
87 27 : _size.setWidth(e.readDouble() * .5 / PPI);
88 : } else {
89 0 : e.unknown();
90 : }
91 : }
92 27 : double w1 = _size.width() - _oddLeftMargin - _oddRightMargin;
93 27 : double w2 = _size.width() - _evenLeftMargin - _evenRightMargin;
94 27 : _printableWidth = std::min(w1, w2); // silently adjust right margins
95 27 : }
96 :
97 53 : void initPageFormat(MStyle* style, PageFormat* pf)
98 : {
99 53 : SizeF sz;
100 53 : sz.setWidth(style->value(Sid::pageWidth).toReal());
101 53 : sz.setHeight(style->value(Sid::pageHeight).toReal());
102 53 : pf->setSize(sz);
103 53 : pf->setPrintableWidth(style->value(Sid::pagePrintableWidth).toReal());
104 53 : pf->setEvenLeftMargin(style->value(Sid::pageEvenLeftMargin).toReal());
105 53 : pf->setOddLeftMargin(style->value(Sid::pageOddLeftMargin).toReal());
106 53 : pf->setEvenTopMargin(style->value(Sid::pageEvenTopMargin).toReal());
107 53 : pf->setEvenBottomMargin(style->value(Sid::pageEvenBottomMargin).toReal());
108 53 : pf->setOddTopMargin(style->value(Sid::pageOddTopMargin).toReal());
109 53 : pf->setOddBottomMargin(style->value(Sid::pageOddBottomMargin).toReal());
110 53 : pf->setTwosided(style->value(Sid::pageTwosided).toBool());
111 53 : }
112 :
113 53 : void setPageFormat(MStyle* style, const PageFormat& pf)
114 : {
115 53 : style->set(Sid::pageWidth, pf.size().width());
116 53 : style->set(Sid::pageHeight, pf.size().height());
117 53 : style->set(Sid::pagePrintableWidth, pf.printableWidth());
118 53 : style->set(Sid::pageEvenLeftMargin, pf.evenLeftMargin());
119 53 : style->set(Sid::pageOddLeftMargin, pf.oddLeftMargin());
120 53 : style->set(Sid::pageEvenTopMargin, pf.evenTopMargin());
121 53 : style->set(Sid::pageEvenBottomMargin, pf.evenBottomMargin());
122 53 : style->set(Sid::pageOddTopMargin, pf.oddTopMargin());
123 53 : style->set(Sid::pageOddBottomMargin, pf.oddBottomMargin());
124 53 : style->set(Sid::pageTwosided, pf.twosided());
125 53 : }
126 :
127 27 : void readPageFormat206(MStyle* style, XmlReader& e)
128 : {
129 27 : PageFormat pf;
130 27 : initPageFormat(style, &pf);
131 27 : pf.read206(e);
132 27 : setPageFormat(style, pf);
133 27 : }
134 : }
|