prelim tdf#136472: remove implementation specific unit tests
[LibreOffice.git] / vcl / qt5 / QtFontFace.cxx
blob89182d115756ea08ccf012c5aab42ee857e2b956
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 <sft.hxx>
29 #include <impfontcharmap.hxx>
30 #include <fontinstance.hxx>
31 #include <font/FontSelectPattern.hxx>
32 #include <font/PhysicalFontCollection.hxx>
34 #include <QtGui/QFont>
35 #include <QtGui/QFontDatabase>
36 #include <QtGui/QFontInfo>
37 #include <QtGui/QRawFont>
39 using namespace vcl;
41 QtFontFace::QtFontFace(const QtFontFace& rSrc)
42 : vcl::font::PhysicalFontFace(rSrc)
43 , m_aFontId(rSrc.m_aFontId)
44 , m_eFontIdType(rSrc.m_eFontIdType)
46 if (rSrc.m_xCharMap.is())
47 m_xCharMap = rSrc.m_xCharMap;
50 FontWeight QtFontFace::toFontWeight(const int nWeight)
52 if (nWeight <= QFont::Thin)
53 return WEIGHT_THIN;
54 if (nWeight <= QFont::ExtraLight)
55 return WEIGHT_ULTRALIGHT;
56 if (nWeight <= QFont::Light)
57 return WEIGHT_LIGHT;
58 if (nWeight <= QFont::Normal)
59 return WEIGHT_NORMAL;
60 if (nWeight <= QFont::Medium)
61 return WEIGHT_MEDIUM;
62 if (nWeight <= QFont::DemiBold)
63 return WEIGHT_SEMIBOLD;
64 if (nWeight <= QFont::Bold)
65 return WEIGHT_BOLD;
66 if (nWeight <= QFont::ExtraBold)
67 return WEIGHT_ULTRABOLD;
68 return WEIGHT_BLACK;
71 FontWidth QtFontFace::toFontWidth(const int nStretch)
73 if (nStretch == 0) // QFont::AnyStretch since Qt 5.8
74 return WIDTH_DONTKNOW;
75 if (nStretch <= QFont::UltraCondensed)
76 return WIDTH_ULTRA_CONDENSED;
77 if (nStretch <= QFont::ExtraCondensed)
78 return WIDTH_EXTRA_CONDENSED;
79 if (nStretch <= QFont::Condensed)
80 return WIDTH_CONDENSED;
81 if (nStretch <= QFont::SemiCondensed)
82 return WIDTH_SEMI_CONDENSED;
83 if (nStretch <= QFont::Unstretched)
84 return WIDTH_NORMAL;
85 if (nStretch <= QFont::SemiExpanded)
86 return WIDTH_SEMI_EXPANDED;
87 if (nStretch <= QFont::Expanded)
88 return WIDTH_EXPANDED;
89 if (nStretch <= QFont::ExtraExpanded)
90 return WIDTH_EXTRA_EXPANDED;
91 return WIDTH_ULTRA_EXPANDED;
94 FontItalic QtFontFace::toFontItalic(const QFont::Style eStyle)
96 switch (eStyle)
98 case QFont::StyleNormal:
99 return ITALIC_NONE;
100 case QFont::StyleItalic:
101 return ITALIC_NORMAL;
102 case QFont::StyleOblique:
103 return ITALIC_OBLIQUE;
106 return ITALIC_NONE;
109 void QtFontFace::fillAttributesFromQFont(const QFont& rFont, FontAttributes& rFA)
111 QFontInfo aFontInfo(rFont);
113 rFA.SetFamilyName(toOUString(aFontInfo.family()));
114 if (IsStarSymbol(toOUString(aFontInfo.family())))
115 rFA.SetSymbolFlag(true);
116 rFA.SetStyleName(toOUString(aFontInfo.styleName()));
117 rFA.SetPitch(aFontInfo.fixedPitch() ? PITCH_FIXED : PITCH_VARIABLE);
118 rFA.SetWeight(QtFontFace::toFontWeight(aFontInfo.weight()));
119 rFA.SetItalic(QtFontFace::toFontItalic(aFontInfo.style()));
120 rFA.SetWidthType(QtFontFace::toFontWidth(rFont.stretch()));
123 QtFontFace* QtFontFace::fromQFont(const QFont& rFont)
125 FontAttributes aFA;
126 fillAttributesFromQFont(rFont, aFA);
127 return new QtFontFace(aFA, rFont.toString(), FontIdType::Font);
130 QtFontFace* QtFontFace::fromQFontDatabase(const QString& aFamily, const QString& aStyle)
132 QFontDatabase aFDB;
133 FontAttributes aFA;
135 aFA.SetFamilyName(toOUString(aFamily));
136 if (IsStarSymbol(aFA.GetFamilyName()))
137 aFA.SetSymbolFlag(true);
138 aFA.SetStyleName(toOUString(aStyle));
139 aFA.SetPitch(aFDB.isFixedPitch(aFamily, aStyle) ? PITCH_FIXED : PITCH_VARIABLE);
140 aFA.SetWeight(QtFontFace::toFontWeight(aFDB.weight(aFamily, aStyle)));
141 aFA.SetItalic(aFDB.italic(aFamily, aStyle) ? ITALIC_NORMAL : ITALIC_NONE);
143 int nPointSize = 0;
144 QList<int> aPointList = aFDB.pointSizes(aFamily, aStyle);
145 if (!aPointList.empty())
146 nPointSize = aPointList[0];
148 return new QtFontFace(aFA, aFamily + "," + aStyle + "," + QString::number(nPointSize),
149 FontIdType::FontDB);
152 QtFontFace::QtFontFace(const FontAttributes& rFA, const QString& rFontID,
153 const FontIdType eFontIdType)
154 : PhysicalFontFace(rFA)
155 , m_aFontId(rFontID)
156 , m_eFontIdType(eFontIdType)
157 , m_bFontCapabilitiesRead(false)
161 sal_IntPtr QtFontFace::GetFontId() const { return reinterpret_cast<sal_IntPtr>(&m_aFontId); }
163 QFont QtFontFace::CreateFont() const
165 QFont aFont;
166 switch (m_eFontIdType)
168 case FontDB:
170 QFontDatabase aFDB;
171 QStringList aStrList = m_aFontId.split(",");
172 if (3 == aStrList.size())
173 aFont = aFDB.font(aStrList[0], aStrList[1], aStrList[2].toInt());
174 else
175 SAL_WARN("vcl.qt", "Invalid QFontDatabase font ID " << m_aFontId);
176 break;
178 case Font:
179 bool bRet = aFont.fromString(m_aFontId);
180 SAL_WARN_IF(!bRet, "vcl.qt", "Failed to create QFont from ID: " << m_aFontId);
181 Q_UNUSED(bRet);
182 break;
184 return aFont;
187 rtl::Reference<LogicalFontInstance>
188 QtFontFace::CreateFontInstance(const vcl::font::FontSelectPattern& rFSD) const
190 return new QtFont(*this, rFSD);
193 FontCharMapRef QtFontFace::GetFontCharMap() const
195 if (m_xCharMap.is())
196 return m_xCharMap;
198 QFont aFont = CreateFont();
199 QRawFont aRawFont(QRawFont::fromFont(aFont));
200 QByteArray aCMapTable = aRawFont.fontTable("cmap");
201 if (aCMapTable.isEmpty())
203 m_xCharMap = new FontCharMap();
204 return m_xCharMap;
207 CmapResult aCmapResult;
208 if (ParseCMAP(reinterpret_cast<const unsigned char*>(aCMapTable.data()), aCMapTable.size(),
209 aCmapResult))
210 m_xCharMap = new FontCharMap(aCmapResult);
212 return m_xCharMap;
215 bool QtFontFace::GetFontCapabilities(vcl::FontCapabilities& rFontCapabilities) const
217 // read this only once per font
218 if (m_bFontCapabilitiesRead)
220 rFontCapabilities = m_aFontCapabilities;
221 return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
223 m_bFontCapabilitiesRead = true;
225 QFont aFont = CreateFont();
226 QRawFont aRawFont(QRawFont::fromFont(aFont));
227 QByteArray aOS2Table = aRawFont.fontTable("OS/2");
228 if (!aOS2Table.isEmpty())
230 vcl::getTTCoverage(m_aFontCapabilities.oUnicodeRange, m_aFontCapabilities.oCodePageRange,
231 reinterpret_cast<const unsigned char*>(aOS2Table.data()),
232 aOS2Table.size());
235 rFontCapabilities = m_aFontCapabilities;
236 return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */