File: musicxmlfonthandler.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg2-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,932 kB
  • sloc: cpp: 264,610; xml: 176,707; sh: 3,311; ansic: 1,384; python: 356; makefile: 188; perl: 82; pascal: 78
file content (299 lines) | stat: -rw-r--r-- 11,178 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//=============================================================================
//  MusE Score
//  Linux Music Score Editor
//
//  Copyright (C) 2014 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

/**
 MusicXML font handling support.
 */

#include "libmscore/sym.h"
#include "libmscore/xml.h"
#include "musicxmlfonthandler.h"

namespace Ms {

//---------------------------------------------------------
//   charFormat2QString
//    convert charFormat to QString for debug print
//---------------------------------------------------------

#if 0
static QString charFormat2QString(const CharFormat& f)
      {
      return QString("b %1 i %2 u %3 va %4 fs %5 fam %6")
            .arg(f.bold())
            .arg(f.italic())
            .arg(f.underline())
            .arg(static_cast<int>(f.valign()))
            .arg(f.fontSize())
            .arg(f.fontFamily())
                 ;
      }

void dumpText(const QList<TextFragment>& list)
      {
      qDebug("MScoreTextToMXML::dumpText %d fragment(s)", list.size());
      for (const TextFragment& f : list) {
            QString t = "fragment";
            if (f.format.type() == CharFormatType::TEXT) {
                  t += QString(" text '%1'").arg(f.text);
                  t += QString(" len %1").arg(f.text.size());
                  }
            else {
                  t += " syms";
                  int len = 0;
                  for (const SymId id : f.ids) {
                        t += QString(" '%1'").arg(Sym::id2name(id));
                        QString s = QString("<sym>%1</sym>").arg(Sym::id2name(id));
                        len += s.size();
                        }
                  t += QString(" len %1").arg(len);
                  }
            t += " format ";
            t += charFormat2QString(f.format);
            qDebug("%s", qPrintable(t));
            }
      }
#endif

//---------------------------------------------------------
//   MScoreTextToMXML
//---------------------------------------------------------

MScoreTextToMXML::MScoreTextToMXML(const QString& tag, const QString& attr, const CharFormat& defFmt, const QString& mtf)
      : attribs(attr), tagname(tag), oldFormat(defFmt), musicalTextFont(mtf)
      {
      // set MusicXML defaults
      oldFormat.setBold(false);
      oldFormat.setItalic(false);
      oldFormat.setUnderline(false);
      }

//---------------------------------------------------------
//   toPlainText
//    convert to plain text
//    naive implementation: simply remove all chars from '<' to '>'
//    typically used to remove formatting info from fields read
//    from MuseScore 1.3 file where they are stored as html, such as
//    part name and shortName
//---------------------------------------------------------

QString MScoreTextToMXML::toPlainText(const QString& text)
      {
      QString res;
      bool inElem = false;
      foreach(QChar ch, text) {
            if (ch == '<')
                  inElem = true;
            else if (ch == '>')
                  inElem = false;
            else {
                  if (!inElem)
                        res += ch;
                  }
            }
      //qDebug("MScoreTextToMXML::toPlainText('%s') res '%s'", qPrintable(text), qPrintable(res));
      return res;
      }
      
//---------------------------------------------------------
//   toPlainTextPlusSymbols
//    convert to plain text plus <sym>[name]</sym> encoded symbols
//---------------------------------------------------------

QString MScoreTextToMXML::toPlainTextPlusSymbols(const QList<TextFragment>& list)
      {
      QString res;
      for (const TextFragment& f : list) {
            if (f.format.type() == CharFormatType::TEXT)
                  res += f.text;
            else {
                  for (const SymId id : f.ids)
                        res += QString("<sym>%1</sym>").arg(Sym::id2name(id));
                  }
            }
      return res;
      }

//---------------------------------------------------------
//   plainTextPlusSymbolsSize
//---------------------------------------------------------

static int plainTextPlusSymbolsFragmentSize(const TextFragment& f)
      {
      int res = 0;
      if (f.format.type() == CharFormatType::TEXT)
            res += f.columns();
      else {
            for (const SymId id : f.ids)
                  res += QString("<sym>%1</sym>").arg(Sym::id2name(id)).size();
            }
      return res;
      }

//---------------------------------------------------------
//   plainTextPlusSymbolsSize
//---------------------------------------------------------

static int plainTextPlusSymbolsListSize(const QList<TextFragment>& list)
      {
      int res = 0;
      for (const TextFragment& f : list) {
            res += plainTextPlusSymbolsFragmentSize(f);
            }
      return res;
      }

//---------------------------------------------------------
//   split
//---------------------------------------------------------

/**
 Split \a in into \a left, \a mid and \a right. Mid starts at \a pos and is \a len characters long.
 Pos and len refer to the representation returned by toPlainTextPlusSymbols().
 TODO Make sure surrogate pairs are handled correctly
 Return true if OK, false on error.
 */

bool MScoreTextToMXML::split(const QList<TextFragment>& in, const int pos, const int len,
                             QList<TextFragment>& left, QList<TextFragment>& mid, QList<TextFragment>& right)
      {
      //qDebug("MScoreTextToMXML::split in size %d pos %d len %d", plainTextPlusSymbolsListSize(in), pos, len);
      //qDebug("-> in");
      //dumpText(in);

      if (pos < 0 || len < 0)
            return false;

      // ensure output is empty at start
      left.clear();
      mid.clear();
      right.clear();

      // set pos to begin of first fragment
      int fragmentNr = 0;
      TextFragment fragment;
      if (fragmentNr < in.size()) fragment = in.at(fragmentNr);
      QList<TextFragment>* currentDest = &left;
      int currentMaxSize = pos;

      // while text left
      while (fragmentNr < in.size()) {
            int destSize = plainTextPlusSymbolsListSize(*currentDest);
            int fragSize = plainTextPlusSymbolsFragmentSize(fragment);
            // if no room left in current destination (check applies only to left and mid)
            if ((currentDest != &right && destSize >= currentMaxSize)
                || currentDest == &right) {
                  // move to next destination
                  if (currentDest == &left) {
                        currentDest = &mid;
                        currentMaxSize = len;
                        }
                  else if (currentDest == &mid) {
                        currentDest = &right;
                        }
                  }
            // if current fragment fits in current destination (check applies only to left and mid)
            if ((currentDest != &right && destSize + fragSize <= currentMaxSize)
                || currentDest == &right) {
                  // add it
                  currentDest->append(fragment);
                  // move to next fragment
                  fragmentNr++;
                  if (fragmentNr < in.size()) fragment = in.at(fragmentNr);
                  }
            else {
                  // split current fragment
                  TextFragment rightPart = fragment.split(currentMaxSize - plainTextPlusSymbolsListSize(*currentDest));
                  // add first part to current destination
                  currentDest->append(fragment);
                  fragment = rightPart;
                  }
            }

      /*
      qDebug("-> left");
      dumpText(left);
      qDebug("-> mid");
      dumpText(mid);
      qDebug("-> right");
      dumpText(right);
       */

      return true;
      }
      
//---------------------------------------------------------
//   writeTextFragments
//---------------------------------------------------------

void MScoreTextToMXML::writeTextFragments(const QList<TextFragment>& fr, Xml& xml)
      {
      //qDebug("MScoreTextToMXML::writeTextFragments defFmt %s", qPrintable(charFormat2QString(oldFormat)));
      //dumpText(fr);
      bool firstTime = true; // write additional attributes only the first time characters are written
      for (const TextFragment& f : fr) {
            newFormat = f.format;
            if (f.format.type() == CharFormatType::SYMBOL) {
                  // for symbols, only an explicit font change is required
                  // fragment text is already SMuFL-compliant
                  newFormat.setFontFamily(musicalTextFont);
                  }
            QString formatAttr = updateFormat();
            xml.tag(tagname + (firstTime ? attribs : "") + formatAttr, f.text);
            firstTime = false;
            }
      }

//---------------------------------------------------------
//   attribute
//    add one attribute if necessary
//---------------------------------------------------------

static QString attribute(bool needed, bool value, QString trueString, QString falseString)
      {
      QString res;
      if (needed)
            res = value ? trueString : falseString;
      if (res != "")
            res = " " + res;
      return res;
      }

//---------------------------------------------------------
//   updateFormat
//    update the text format by generating attributes
//    corresponding to the difference between old- and newFormat
//    copy newFormat to oldFormat
//---------------------------------------------------------

QString MScoreTextToMXML::updateFormat()
      {
      QString res;
      res += attribute(newFormat.bold() != oldFormat.bold(), newFormat.bold(), "font-weight=\"bold\"", "font-weight=\"normal\"");
      res += attribute(newFormat.italic() != oldFormat.italic(), newFormat.italic(), "font-style=\"italic\"", "font-style=\"normal\"");
      res += attribute(newFormat.underline() != oldFormat.underline(), newFormat.underline(), "underline=\"1\"", "underline=\"0\"");
      res += attribute(newFormat.fontFamily() != oldFormat.fontFamily(), true, QString("font-family=\"%1\"").arg(newFormat.fontFamily()), "");
      bool needSize = newFormat.fontSize() < 0.99 * oldFormat.fontSize() || newFormat.fontSize() > 1.01 * oldFormat.fontSize();
      res += attribute(needSize, true, QString("font-size=\"%1\"").arg(newFormat.fontSize()), "");
      //qDebug("updateFormat() res '%s'", qPrintable(res));
      oldFormat = newFormat;
      return res;
      }

} // namespace Ms