android: file-cache to improve performance.
[LibreOffice.git] / vcl / qt5 / Qt5FontFace.cxx
bloba7837667f1dd76ca1fbfa2b8ddc85ec57ba63867
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 <Qt5FontFace.hxx>
25 #include <Qt5Font.hxx>
26 #include <Qt5Tools.hxx>
28 #include <sft.hxx>
29 #include <impfontcharmap.hxx>
30 #include <fontinstance.hxx>
31 #include <fontselect.hxx>
32 #include <PhysicalFontCollection.hxx>
34 #include <QtGui/QFont>
35 #include <QtGui/QFontDatabase>
36 #include <QtGui/QFontInfo>
37 #include <QtGui/QRawFont>
39 using namespace vcl;
41 Qt5FontFace::Qt5FontFace(const Qt5FontFace& rSrc)
42 : PhysicalFontFace(rSrc)
43 , m_aFontId(rSrc.m_aFontId)
45 if (rSrc.m_xCharMap.is())
46 m_xCharMap = rSrc.m_xCharMap;
49 static FontWeight fromQFontWeight(int nWeight)
51 FontWeight eWeight = WEIGHT_DONTKNOW;
52 switch (nWeight)
54 case QFont::Thin:
55 eWeight = WEIGHT_THIN;
56 break;
57 case QFont::ExtraLight:
58 eWeight = WEIGHT_ULTRALIGHT;
59 break;
60 case QFont::Light:
61 eWeight = WEIGHT_LIGHT;
62 break;
63 case QFont::Normal:
64 eWeight = WEIGHT_NORMAL;
65 break;
66 case QFont::Medium:
67 eWeight = WEIGHT_MEDIUM;
68 break;
69 case QFont::DemiBold:
70 eWeight = WEIGHT_SEMIBOLD;
71 break;
72 case QFont::Bold:
73 eWeight = WEIGHT_BOLD;
74 break;
75 case QFont::ExtraBold:
76 eWeight = WEIGHT_ULTRABOLD;
77 break;
78 case QFont::Black:
79 eWeight = WEIGHT_BLACK;
80 break;
82 return eWeight;
85 void Qt5FontFace::fillAttributesFromQFont(const QFont& rFont, FontAttributes& rFA)
87 QFontInfo aFontInfo(rFont);
89 rFA.SetFamilyName(toOUString(aFontInfo.family()));
90 if (IsStarSymbol(toOUString(aFontInfo.family())))
91 rFA.SetSymbolFlag(true);
92 rFA.SetStyleName(toOUString(aFontInfo.styleName()));
93 rFA.SetPitch(aFontInfo.fixedPitch() ? PITCH_FIXED : PITCH_VARIABLE);
94 rFA.SetWeight(fromQFontWeight(aFontInfo.weight()));
96 switch (aFontInfo.style())
98 case QFont::StyleNormal:
99 rFA.SetItalic(ITALIC_NONE);
100 break;
101 case QFont::StyleItalic:
102 rFA.SetItalic(ITALIC_NORMAL);
103 break;
104 case QFont::StyleOblique:
105 rFA.SetItalic(ITALIC_OBLIQUE);
106 break;
110 Qt5FontFace* Qt5FontFace::fromQFont(const QFont& rFont)
112 FontAttributes aFA;
113 fillAttributesFromQFont(rFont, aFA);
114 return new Qt5FontFace(aFA, rFont.toString());
117 Qt5FontFace* Qt5FontFace::fromQFontDatabase(const QString& aFamily, const QString& aStyle)
119 QFontDatabase aFDB;
120 FontAttributes aFA;
121 aFA.SetFamilyName(toOUString(aFamily));
122 if (IsStarSymbol(aFA.GetFamilyName()))
123 aFA.SetSymbolFlag(true);
124 aFA.SetStyleName(toOUString(aStyle));
125 aFA.SetPitch(aFDB.isFixedPitch(aFamily, aStyle) ? PITCH_FIXED : PITCH_VARIABLE);
126 aFA.SetWeight(fromQFontWeight(aFDB.weight(aFamily, aStyle)));
127 aFA.SetItalic(aFDB.italic(aFamily, aStyle) ? ITALIC_NORMAL : ITALIC_NONE);
128 return new Qt5FontFace(aFA, aFamily + "," + aStyle);
131 Qt5FontFace::Qt5FontFace(const FontAttributes& rFA, const QString& rFontID)
132 : PhysicalFontFace(rFA)
133 , m_aFontId(rFontID)
134 , m_bFontCapabilitiesRead(false)
138 sal_IntPtr Qt5FontFace::GetFontId() const { return reinterpret_cast<sal_IntPtr>(&m_aFontId); }
140 rtl::Reference<LogicalFontInstance>
141 Qt5FontFace::CreateFontInstance(const FontSelectPattern& rFSD) const
143 return new Qt5Font(*this, rFSD);
146 const FontCharMapRef& Qt5FontFace::GetFontCharMap() const
148 if (m_xCharMap.is())
149 return m_xCharMap;
151 QFont aFont;
152 aFont.fromString(m_aFontId);
153 QRawFont aRawFont(QRawFont::fromFont(aFont));
154 QByteArray aCMapTable = aRawFont.fontTable("cmap");
155 if (aCMapTable.isEmpty())
157 m_xCharMap = new FontCharMap();
158 return m_xCharMap;
161 CmapResult aCmapResult;
162 if (ParseCMAP(reinterpret_cast<const unsigned char*>(aCMapTable.data()), aCMapTable.size(),
163 aCmapResult))
164 m_xCharMap = new FontCharMap(aCmapResult);
166 return m_xCharMap;
169 bool Qt5FontFace::GetFontCapabilities(vcl::FontCapabilities& rFontCapabilities) const
171 // read this only once per font
172 if (m_bFontCapabilitiesRead)
174 rFontCapabilities = m_aFontCapabilities;
175 return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
177 m_bFontCapabilitiesRead = true;
179 QFont aFont;
180 aFont.fromString(m_aFontId);
181 QRawFont aRawFont(QRawFont::fromFont(aFont));
182 QByteArray aOS2Table = aRawFont.fontTable("OS/2");
183 if (!aOS2Table.isEmpty())
185 vcl::getTTCoverage(m_aFontCapabilities.oUnicodeRange, m_aFontCapabilities.oCodePageRange,
186 reinterpret_cast<const unsigned char*>(aOS2Table.data()),
187 aOS2Table.size());
190 rFontCapabilities = m_aFontCapabilities;
191 return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */