Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-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 "qfontprovider.h"
23 :
24 : #include <memory>
25 :
26 : #include "global/io/file.h"
27 :
28 : #include <QFont>
29 : #include <QPaintDevice>
30 : #include <QFontDatabase>
31 : #include <QFontMetricsF>
32 : #include <QRawFont>
33 : #include <QPainterPath>
34 :
35 : using namespace muse;
36 : using namespace muse::draw;
37 :
38 : class FontPaintDevice : public QPaintDevice
39 : {
40 : public:
41 0 : QPaintEngine* paintEngine() const override
42 : {
43 0 : return nullptr;
44 : }
45 :
46 : protected:
47 : static constexpr double MU_ENGRAVING_DPI = 1200; // Same as mu::engraving::DPI. TODO: pass as parameter
48 1122976 : int metric(PaintDeviceMetric m) const override
49 : {
50 1122976 : switch (m) {
51 1122976 : case QPaintDevice::PdmDpiX:
52 : case QPaintDevice::PdmDpiY:
53 1122976 : return MU_ENGRAVING_DPI;
54 0 : default:
55 0 : return 1;
56 : }
57 : }
58 : };
59 :
60 : static FontPaintDevice device;
61 :
62 46 : int QFontProvider::addSymbolFont(const String& family, const io::path_t& path)
63 : {
64 46 : return addSymbolFont(family, std::make_shared<io::File>(path));
65 : }
66 :
67 190 : int QFontProvider::addSymbolFont(const String&, std::shared_ptr<io::IODevice> ioDevice)
68 : {
69 380 : const std::string filePath = ioDevice->meta("file_path");
70 190 : if (!filePath.empty()) {
71 190 : return QFontDatabase::addApplicationFont(QString::fromStdString(filePath));
72 : }
73 :
74 0 : if (!ioDevice->open(io::IODevice::ReadOnly)) {
75 0 : return -1;
76 : }
77 :
78 0 : return QFontDatabase::addApplicationFontFromData(ioDevice->readAll().toQByteArray());
79 190 : }
80 :
81 79301 : double QFontProvider::lineSpacing(const Font& f) const
82 : {
83 79301 : return QFontMetricsF(f.toQFont(), &device).lineSpacing();
84 : }
85 :
86 2170 : double QFontProvider::xHeight(const Font& f) const
87 : {
88 2170 : return QFontMetricsF(f.toQFont(), &device).xHeight();
89 : }
90 :
91 6 : double QFontProvider::height(const Font& f) const
92 : {
93 6 : return QFontMetricsF(f.toQFont(), &device).height();
94 : }
95 :
96 17709 : double QFontProvider::capHeight(const Font& f) const
97 : {
98 17709 : return QFontMetrics(f.toQFont(), &device).capHeight();
99 : }
100 :
101 4504 : double QFontProvider::ascent(const Font& f) const
102 : {
103 4504 : return QFontMetricsF(f.toQFont(), &device).ascent();
104 : }
105 :
106 6 : double QFontProvider::descent(const Font& f) const
107 : {
108 6 : return QFontMetricsF(f.toQFont(), &device).descent();
109 : }
110 :
111 6 : double QFontProvider::underlinePos(const Font& f) const
112 : {
113 6 : return QFontMetricsF(f.toQFont(), &device).underlinePos();
114 : }
115 :
116 6 : double QFontProvider::lineWidth(const Font& f) const
117 : {
118 6 : return QFontMetricsF(f.toQFont(), &device).lineWidth();
119 : }
120 :
121 6 : double QFontProvider::strikeOutPos(const Font& f) const
122 : {
123 6 : return QFontMetricsF(f.toQFont(), &device).strikeOutPos();
124 : }
125 :
126 708426 : bool QFontProvider::inFont(const Font& f, char32_t ucs4) const
127 : {
128 : // NOTE: QFontMetricsF::inFontUcs4 is unreliable for our use case because it uses Qt's fallback
129 : // system even if the flag noFontMerging is set, and returns true if the character
130 : // is found in any of the fallbacks. We need to use instead QRawFont, which represents the
131 : // *actual* font, not Qt's interpretation of the query. From QRawFont we can query the glyph index
132 : // of a character (zero if not in font) and the bounding rectangle of the actual glyph at that index.
133 708426 : QRawFont qRawFont = QRawFont::fromFont(f.toQFont());
134 708426 : int glyphIndex = qRawFont.glyphIndexesForString(QString(QChar::fromUcs4(ucs4)))[0];
135 708426 : if (glyphIndex == 0) {
136 443023 : return false;
137 : }
138 :
139 : //! @NOTE some symbols in fonts dont have glyph. For example U+ee80
140 : //! exists in Bravura.otf but doesn't have glyph
141 : //! so QFontMetricsF returns true in that case
142 265403 : return qRawFont.boundingRect(glyphIndex).isValid();
143 708426 : }
144 :
145 255247 : double QFontProvider::horizontalAdvance(const Font& f, const String& string) const
146 : {
147 255247 : return QFontMetricsF(f.toQFont(), &device).horizontalAdvance(string);
148 : }
149 :
150 156930 : double QFontProvider::horizontalAdvance(const Font& f, char32_t ucs4) const
151 : {
152 156930 : if (Char::requiresSurrogates(ucs4)) {
153 54 : return QFontMetricsF(f.toQFont(), &device).horizontalAdvance(String::fromUcs4(ucs4));
154 : }
155 :
156 156876 : return QFontMetricsF(f.toQFont(), &device).horizontalAdvance(static_cast<char16_t>(ucs4));
157 : }
158 :
159 99 : RectF QFontProvider::boundingRect(const Font& f, const String& string) const
160 : {
161 99 : return RectF::fromQRectF(QFontMetricsF(f.toQFont(), &device).boundingRect(string));
162 : }
163 :
164 156700 : RectF QFontProvider::boundingRect(const Font& f, char32_t ucs4) const
165 : {
166 156700 : if (Char::requiresSurrogates(ucs4)) {
167 54 : return RectF::fromQRectF(QFontMetrics(f.toQFont(), &device).boundingRect(String::fromUcs4(ucs4)));
168 : }
169 :
170 156646 : if (f.type() == Font::Type::MusicSymbol || f.type() == Font::Type::MusicSymbolText) {
171 : // QFontMetrics::boundingRect returns pixel values obtained by rasterization of the font.
172 : // QPainterPath::boundingRect works from the actual vector shape so it's more accurate.
173 : // There is still some discretization going on so we can make it even more accurate by upscaling.
174 : // CAUTION: More expensive! Ok for music glyphs because they are cached.
175 156586 : QFont qf = f.toQFont();
176 156586 : qf = QFont(qf, &device);
177 : static constexpr double UPSCALING = 4.0;
178 156586 : qf.setPointSizeF(qf.pointSizeF() * UPSCALING);
179 156586 : QPainterPath path;
180 156586 : path.addText(QPointF(), qf, QString(static_cast<char16_t>(ucs4)));
181 156586 : return RectF::fromQRectF(path.boundingRect()).scaled(SizeF(1.0 / UPSCALING, 1.0 / UPSCALING));
182 156586 : }
183 :
184 60 : return RectF::fromQRectF(QFontMetricsF(f.toQFont(), &device).boundingRect(static_cast<char16_t>(ucs4)));
185 : }
186 :
187 450286 : RectF QFontProvider::tightBoundingRect(const Font& f, const String& string) const
188 : {
189 450286 : auto boundingRect = QFontMetricsF(f.toQFont(), &device).tightBoundingRect(string);
190 450286 : if (!boundingRect.isValid()) {
191 : // fix for https://github.com/musescore/MuseScore/issues/19503 - Qt can return garbage bounding rectangles that corrupt layout
192 15395 : return RectF();
193 : }
194 434891 : return RectF::fromQRectF(boundingRect);
195 : }
|