1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <unotools/fontdefs.hxx>
23 
24 #include <Qt5FontFace.hxx>
25 #include <Qt5Font.hxx>
26 #include <Qt5Tools.hxx>
27 
28 #include <sft.hxx>
29 #include <impfontcharmap.hxx>
30 #include <fontinstance.hxx>
31 #include <fontselect.hxx>
32 #include <PhysicalFontCollection.hxx>
33 
34 #include <QtGui/QFont>
35 #include <QtGui/QFontDatabase>
36 #include <QtGui/QFontInfo>
37 #include <QtGui/QRawFont>
38 
39 using namespace vcl;
40 
Qt5FontFace(const Qt5FontFace & rSrc)41 Qt5FontFace::Qt5FontFace(const Qt5FontFace& rSrc)
42     : PhysicalFontFace(rSrc)
43     , m_aFontId(rSrc.m_aFontId)
44 {
45     if (rSrc.m_xCharMap.is())
46         m_xCharMap = rSrc.m_xCharMap;
47 }
48 
toFontWeight(const int nWeight)49 FontWeight Qt5FontFace::toFontWeight(const int nWeight)
50 {
51     if (nWeight <= QFont::Thin)
52         return WEIGHT_THIN;
53     if (nWeight <= QFont::ExtraLight)
54         return WEIGHT_ULTRALIGHT;
55     if (nWeight <= QFont::Light)
56         return WEIGHT_LIGHT;
57     if (nWeight <= QFont::Normal)
58         return WEIGHT_NORMAL;
59     if (nWeight <= QFont::Medium)
60         return WEIGHT_MEDIUM;
61     if (nWeight <= QFont::DemiBold)
62         return WEIGHT_SEMIBOLD;
63     if (nWeight <= QFont::Bold)
64         return WEIGHT_BOLD;
65     if (nWeight <= QFont::ExtraBold)
66         return WEIGHT_ULTRABOLD;
67     return WEIGHT_BLACK;
68 }
69 
toFontWidth(const int nStretch)70 FontWidth Qt5FontFace::toFontWidth(const int nStretch)
71 {
72     if (nStretch == 0) // QFont::AnyStretch since Qt 5.8
73         return WIDTH_DONTKNOW;
74     if (nStretch <= QFont::UltraCondensed)
75         return WIDTH_ULTRA_CONDENSED;
76     if (nStretch <= QFont::ExtraCondensed)
77         return WIDTH_EXTRA_CONDENSED;
78     if (nStretch <= QFont::Condensed)
79         return WIDTH_CONDENSED;
80     if (nStretch <= QFont::SemiCondensed)
81         return WIDTH_SEMI_CONDENSED;
82     if (nStretch <= QFont::Unstretched)
83         return WIDTH_NORMAL;
84     if (nStretch <= QFont::SemiExpanded)
85         return WIDTH_SEMI_EXPANDED;
86     if (nStretch <= QFont::Expanded)
87         return WIDTH_EXPANDED;
88     if (nStretch <= QFont::ExtraExpanded)
89         return WIDTH_EXTRA_EXPANDED;
90     return WIDTH_ULTRA_EXPANDED;
91 }
92 
toFontItalic(const QFont::Style eStyle)93 FontItalic Qt5FontFace::toFontItalic(const QFont::Style eStyle)
94 {
95     switch (eStyle)
96     {
97         case QFont::StyleNormal:
98             return ITALIC_NONE;
99         case QFont::StyleItalic:
100             return ITALIC_NORMAL;
101         case QFont::StyleOblique:
102             return ITALIC_OBLIQUE;
103     }
104 
105     return ITALIC_NONE;
106 }
107 
fillAttributesFromQFont(const QFont & rFont,FontAttributes & rFA)108 void Qt5FontFace::fillAttributesFromQFont(const QFont& rFont, FontAttributes& rFA)
109 {
110     QFontInfo aFontInfo(rFont);
111 
112     rFA.SetFamilyName(toOUString(aFontInfo.family()));
113     if (IsStarSymbol(toOUString(aFontInfo.family())))
114         rFA.SetSymbolFlag(true);
115     rFA.SetStyleName(toOUString(aFontInfo.styleName()));
116     rFA.SetPitch(aFontInfo.fixedPitch() ? PITCH_FIXED : PITCH_VARIABLE);
117     rFA.SetWeight(Qt5FontFace::toFontWeight(aFontInfo.weight()));
118     rFA.SetItalic(Qt5FontFace::toFontItalic(aFontInfo.style()));
119     rFA.SetWidthType(Qt5FontFace::toFontWidth(rFont.stretch()));
120 }
121 
fromQFont(const QFont & rFont)122 Qt5FontFace* Qt5FontFace::fromQFont(const QFont& rFont)
123 {
124     FontAttributes aFA;
125     fillAttributesFromQFont(rFont, aFA);
126     return new Qt5FontFace(aFA, rFont.toString());
127 }
128 
fromQFontDatabase(const QString & aFamily,const QString & aStyle)129 Qt5FontFace* Qt5FontFace::fromQFontDatabase(const QString& aFamily, const QString& aStyle)
130 {
131     QFontDatabase aFDB;
132     FontAttributes aFA;
133     aFA.SetFamilyName(toOUString(aFamily));
134     if (IsStarSymbol(aFA.GetFamilyName()))
135         aFA.SetSymbolFlag(true);
136     aFA.SetStyleName(toOUString(aStyle));
137     aFA.SetPitch(aFDB.isFixedPitch(aFamily, aStyle) ? PITCH_FIXED : PITCH_VARIABLE);
138     aFA.SetWeight(Qt5FontFace::toFontWeight(aFDB.weight(aFamily, aStyle)));
139     aFA.SetItalic(aFDB.italic(aFamily, aStyle) ? ITALIC_NORMAL : ITALIC_NONE);
140     return new Qt5FontFace(aFA, aFamily + "," + aStyle);
141 }
142 
Qt5FontFace(const FontAttributes & rFA,const QString & rFontID)143 Qt5FontFace::Qt5FontFace(const FontAttributes& rFA, const QString& rFontID)
144     : PhysicalFontFace(rFA)
145     , m_aFontId(rFontID)
146     , m_bFontCapabilitiesRead(false)
147 {
148 }
149 
GetFontId() const150 sal_IntPtr Qt5FontFace::GetFontId() const { return reinterpret_cast<sal_IntPtr>(&m_aFontId); }
151 
152 rtl::Reference<LogicalFontInstance>
CreateFontInstance(const FontSelectPattern & rFSD) const153 Qt5FontFace::CreateFontInstance(const FontSelectPattern& rFSD) const
154 {
155     return new Qt5Font(*this, rFSD);
156 }
157 
GetFontCharMap() const158 const FontCharMapRef& Qt5FontFace::GetFontCharMap() const
159 {
160     if (m_xCharMap.is())
161         return m_xCharMap;
162 
163     QFont aFont;
164     aFont.fromString(m_aFontId);
165     QRawFont aRawFont(QRawFont::fromFont(aFont));
166     QByteArray aCMapTable = aRawFont.fontTable("cmap");
167     if (aCMapTable.isEmpty())
168     {
169         m_xCharMap = new FontCharMap();
170         return m_xCharMap;
171     }
172 
173     CmapResult aCmapResult;
174     if (ParseCMAP(reinterpret_cast<const unsigned char*>(aCMapTable.data()), aCMapTable.size(),
175                   aCmapResult))
176         m_xCharMap = new FontCharMap(aCmapResult);
177 
178     return m_xCharMap;
179 }
180 
GetFontCapabilities(vcl::FontCapabilities & rFontCapabilities) const181 bool Qt5FontFace::GetFontCapabilities(vcl::FontCapabilities& rFontCapabilities) const
182 {
183     // read this only once per font
184     if (m_bFontCapabilitiesRead)
185     {
186         rFontCapabilities = m_aFontCapabilities;
187         return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
188     }
189     m_bFontCapabilitiesRead = true;
190 
191     QFont aFont;
192     aFont.fromString(m_aFontId);
193     QRawFont aRawFont(QRawFont::fromFont(aFont));
194     QByteArray aOS2Table = aRawFont.fontTable("OS/2");
195     if (!aOS2Table.isEmpty())
196     {
197         vcl::getTTCoverage(m_aFontCapabilities.oUnicodeRange, m_aFontCapabilities.oCodePageRange,
198                            reinterpret_cast<const unsigned char*>(aOS2Table.data()),
199                            aOS2Table.size());
200     }
201 
202     rFontCapabilities = m_aFontCapabilities;
203     return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
204 }
205 
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
207