calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / vcl / qt5 / QtFontFace.cxx
blob351f59739544da3d767e9ef13ac56c4dbe5d363f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <unotools/fontdefs.hxx>
24 #include <QtFontFace.hxx>
25 #include <QtFont.hxx>
26 #include <QtTools.hxx>
28 #include <font/LogicalFontInstance.hxx>
29 #include <font/FontSelectPattern.hxx>
30 #include <font/PhysicalFontCollection.hxx>
32 #include <QtGui/QFont>
33 #include <QtGui/QFontDatabase>
34 #include <QtGui/QFontInfo>
35 #include <QtGui/QRawFont>
36 #include <utility>
38 using namespace vcl;
40 QtFontFace::QtFontFace(const QtFontFace& rSrc)
41 : vcl::font::PhysicalFontFace(rSrc)
42 , m_aFontId(rSrc.m_aFontId)
43 , m_eFontIdType(rSrc.m_eFontIdType)
47 FontWeight QtFontFace::toFontWeight(const int nWeight)
49 if (nWeight <= QFont::Thin)
50 return WEIGHT_THIN;
51 if (nWeight <= QFont::ExtraLight)
52 return WEIGHT_ULTRALIGHT;
53 if (nWeight <= QFont::Light)
54 return WEIGHT_LIGHT;
55 if (nWeight <= QFont::Normal)
56 return WEIGHT_NORMAL;
57 if (nWeight <= QFont::Medium)
58 return WEIGHT_MEDIUM;
59 if (nWeight <= QFont::DemiBold)
60 return WEIGHT_SEMIBOLD;
61 if (nWeight <= QFont::Bold)
62 return WEIGHT_BOLD;
63 if (nWeight <= QFont::ExtraBold)
64 return WEIGHT_ULTRABOLD;
65 return WEIGHT_BLACK;
68 FontWidth QtFontFace::toFontWidth(const int nStretch)
70 if (nStretch == 0) // QFont::AnyStretch since Qt 5.8
71 return WIDTH_DONTKNOW;
72 if (nStretch <= QFont::UltraCondensed)
73 return WIDTH_ULTRA_CONDENSED;
74 if (nStretch <= QFont::ExtraCondensed)
75 return WIDTH_EXTRA_CONDENSED;
76 if (nStretch <= QFont::Condensed)
77 return WIDTH_CONDENSED;
78 if (nStretch <= QFont::SemiCondensed)
79 return WIDTH_SEMI_CONDENSED;
80 if (nStretch <= QFont::Unstretched)
81 return WIDTH_NORMAL;
82 if (nStretch <= QFont::SemiExpanded)
83 return WIDTH_SEMI_EXPANDED;
84 if (nStretch <= QFont::Expanded)
85 return WIDTH_EXPANDED;
86 if (nStretch <= QFont::ExtraExpanded)
87 return WIDTH_EXTRA_EXPANDED;
88 return WIDTH_ULTRA_EXPANDED;
91 FontItalic QtFontFace::toFontItalic(const QFont::Style eStyle)
93 switch (eStyle)
95 case QFont::StyleNormal:
96 return ITALIC_NONE;
97 case QFont::StyleItalic:
98 return ITALIC_NORMAL;
99 case QFont::StyleOblique:
100 return ITALIC_OBLIQUE;
103 return ITALIC_NONE;
106 void QtFontFace::fillAttributesFromQFont(const QFont& rFont, FontAttributes& rFA)
108 QFontInfo aFontInfo(rFont);
110 rFA.SetFamilyName(toOUString(aFontInfo.family()));
111 rFA.SetStyleName(toOUString(aFontInfo.styleName()));
112 rFA.SetPitch(aFontInfo.fixedPitch() ? PITCH_FIXED : PITCH_VARIABLE);
113 rFA.SetWeight(QtFontFace::toFontWeight(aFontInfo.weight()));
114 rFA.SetItalic(QtFontFace::toFontItalic(aFontInfo.style()));
115 rFA.SetWidthType(QtFontFace::toFontWidth(rFont.stretch()));
118 QtFontFace* QtFontFace::fromQFont(const QFont& rFont)
120 FontAttributes aFA;
121 fillAttributesFromQFont(rFont, aFA);
122 return new QtFontFace(aFA, rFont.toString(), FontIdType::Font);
125 QtFontFace* QtFontFace::fromQFontDatabase(const QString& aFamily, const QString& aStyle)
127 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
128 auto const isFixedPitch = QFontDatabase::isFixedPitch(aFamily, aStyle);
129 auto const weigh = QFontDatabase::weight(aFamily, aStyle);
130 auto const italic = QFontDatabase::italic(aFamily, aStyle);
131 auto const aPointList = QFontDatabase::pointSizes(aFamily, aStyle);
132 #else
133 QFontDatabase aFDB;
134 auto const isFixedPitch = aFDB.isFixedPitch(aFamily, aStyle);
135 auto const weigh = aFDB.weight(aFamily, aStyle);
136 auto const italic = aFDB.italic(aFamily, aStyle);
137 auto const aPointList = aFDB.pointSizes(aFamily, aStyle);
138 #endif
140 FontAttributes aFA;
142 aFA.SetFamilyName(toOUString(aFamily));
143 aFA.SetStyleName(toOUString(aStyle));
144 aFA.SetPitch(isFixedPitch ? PITCH_FIXED : PITCH_VARIABLE);
145 aFA.SetWeight(QtFontFace::toFontWeight(weigh));
146 aFA.SetItalic(italic ? ITALIC_NORMAL : ITALIC_NONE);
148 int nPointSize = 0;
149 if (!aPointList.empty())
150 nPointSize = aPointList[0];
152 return new QtFontFace(aFA, aFamily + "," + aStyle + "," + QString::number(nPointSize),
153 FontIdType::FontDB);
156 QtFontFace::QtFontFace(const FontAttributes& rFA, QString aFontID, const FontIdType eFontIdType)
157 : PhysicalFontFace(rFA)
158 , m_aFontId(std::move(aFontID))
159 , m_eFontIdType(eFontIdType)
163 sal_IntPtr QtFontFace::GetFontId() const { return reinterpret_cast<sal_IntPtr>(&m_aFontId); }
165 QFont QtFontFace::CreateFont() const
167 QFont aFont;
168 switch (m_eFontIdType)
170 case FontDB:
172 QStringList aStrList = m_aFontId.split(",");
173 if (3 == aStrList.size())
175 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
176 aFont = QFontDatabase::font(aStrList[0], aStrList[1], aStrList[2].toInt());
177 #else
178 QFontDatabase aFDB;
179 aFont = aFDB.font(aStrList[0], aStrList[1], aStrList[2].toInt());
180 #endif
182 else
183 SAL_WARN("vcl.qt", "Invalid QFontDatabase font ID " << m_aFontId);
184 break;
186 case Font:
187 bool bRet = aFont.fromString(m_aFontId);
188 SAL_WARN_IF(!bRet, "vcl.qt", "Failed to create QFont from ID: " << m_aFontId);
189 Q_UNUSED(bRet);
190 break;
192 return aFont;
195 rtl::Reference<LogicalFontInstance>
196 QtFontFace::CreateFontInstance(const vcl::font::FontSelectPattern& rFSD) const
198 return new QtFont(*this, rFSD);
201 hb_blob_t* QtFontFace::GetHbTable(hb_tag_t nTag) const
203 char pTagName[5] = { '\0' };
204 hb_tag_to_string(nTag, pTagName);
206 QFont aFont = CreateFont();
207 QRawFont aRawFont(QRawFont::fromFont(aFont));
208 QByteArray aTable = aRawFont.fontTable(pTagName);
209 const sal_uInt32 nLength = aTable.size();
211 hb_blob_t* pBlob = nullptr;
212 if (nLength > 0)
213 pBlob = hb_blob_create(aTable.data(), nLength, HB_MEMORY_MODE_DUPLICATE, nullptr, nullptr);
214 return pBlob;
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */