| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 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 <QtFontFace.hxx> |
| 23 | #include <QtFont.hxx> |
| 24 | #include <QtTools.hxx> |
| 25 | |
| 26 | #include <font/LogicalFontInstance.hxx> |
| 27 | |
| 28 | #include <QtGui/QFont> |
| 29 | #include <QtGui/QFontDatabase> |
| 30 | #include <QtGui/QFontInfo> |
| 31 | #include <QtGui/QRawFont> |
| 32 | #include <utility> |
| 33 | |
| 34 | using namespace vcl; |
| 35 | |
| 36 | QtFontFace::QtFontFace(const QtFontFace& rSrc) |
| 37 | : vcl::font::PhysicalFontFace(rSrc) |
| 38 | , m_aFontId(rSrc.m_aFontId) |
| 39 | , m_eFontIdType(rSrc.m_eFontIdType) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | FontWeight QtFontFace::toFontWeight(const int nWeight) |
| 44 | { |
| 45 | if (nWeight <= QFont::Thin) |
| 46 | return WEIGHT_THIN; |
| 47 | if (nWeight <= QFont::ExtraLight) |
| 48 | return WEIGHT_ULTRALIGHT; |
| 49 | if (nWeight <= QFont::Light) |
| 50 | return WEIGHT_LIGHT; |
| 51 | if (nWeight <= QFont::Normal) |
| 52 | return WEIGHT_NORMAL; |
| 53 | if (nWeight <= QFont::Medium) |
| 54 | return WEIGHT_MEDIUM; |
| 55 | if (nWeight <= QFont::DemiBold) |
| 56 | return WEIGHT_SEMIBOLD; |
| 57 | if (nWeight <= QFont::Bold) |
| 58 | return WEIGHT_BOLD; |
| 59 | if (nWeight <= QFont::ExtraBold) |
| 60 | return WEIGHT_ULTRABOLD; |
| 61 | return WEIGHT_BLACK; |
| 62 | } |
| 63 | |
| 64 | FontWidth QtFontFace::toFontWidth(const int nStretch) |
| 65 | { |
| 66 | if (nStretch == 0) // QFont::AnyStretch since Qt 5.8 |
| 67 | return WIDTH_DONTKNOW; |
| 68 | if (nStretch <= QFont::UltraCondensed) |
| 69 | return WIDTH_ULTRA_CONDENSED; |
| 70 | if (nStretch <= QFont::ExtraCondensed) |
| 71 | return WIDTH_EXTRA_CONDENSED; |
| 72 | if (nStretch <= QFont::Condensed) |
| 73 | return WIDTH_CONDENSED; |
| 74 | if (nStretch <= QFont::SemiCondensed) |
| 75 | return WIDTH_SEMI_CONDENSED; |
| 76 | if (nStretch <= QFont::Unstretched) |
| 77 | return WIDTH_NORMAL; |
| 78 | if (nStretch <= QFont::SemiExpanded) |
| 79 | return WIDTH_SEMI_EXPANDED; |
| 80 | if (nStretch <= QFont::Expanded) |
| 81 | return WIDTH_EXPANDED; |
| 82 | if (nStretch <= QFont::ExtraExpanded) |
| 83 | return WIDTH_EXTRA_EXPANDED; |
| 84 | return WIDTH_ULTRA_EXPANDED; |
| 85 | } |
| 86 | |
| 87 | FontItalic QtFontFace::toFontItalic(const QFont::Style eStyle) |
| 88 | { |
| 89 | switch (eStyle) |
| 90 | { |
| 91 | case QFont::StyleNormal: |
| 92 | return ITALIC_NONE; |
| 93 | case QFont::StyleItalic: |
| 94 | return ITALIC_NORMAL; |
| 95 | case QFont::StyleOblique: |
| 96 | return ITALIC_OBLIQUE; |
| 97 | } |
| 98 | |
| 99 | return ITALIC_NONE; |
| 100 | } |
| 101 | |
| 102 | void QtFontFace::fillAttributesFromQFont(const QFont& rFont, FontAttributes& rFA) |
| 103 | { |
| 104 | QFontInfo aFontInfo(rFont); |
| 105 | |
| 106 | rFA.SetFamilyName(toOUString(aFontInfo.family())); |
| 107 | rFA.SetStyleName(toOUString(aFontInfo.styleName())); |
| 108 | rFA.SetPitch(aFontInfo.fixedPitch() ? PITCH_FIXED : PITCH_VARIABLE); |
| 109 | rFA.SetWeight(QtFontFace::toFontWeight(aFontInfo.weight())); |
| 110 | rFA.SetItalic(QtFontFace::toFontItalic(aFontInfo.style())); |
| 111 | rFA.SetWidthType(QtFontFace::toFontWidth(rFont.stretch())); |
| 112 | } |
| 113 | |
| 114 | QtFontFace* QtFontFace::fromQFont(const QFont& rFont) |
| 115 | { |
| 116 | FontAttributes aFA; |
| 117 | fillAttributesFromQFont(rFont, aFA); |
| 118 | return new QtFontFace(aFA, rFont.toString(), FontIdType::Font); |
| 119 | } |
| 120 | |
| 121 | QtFontFace* QtFontFace::fromQFontDatabase(const QString& aFamily, const QString& aStyle) |
| 122 | { |
| 123 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 124 | auto const isFixedPitch = QFontDatabase::isFixedPitch(aFamily, aStyle); |
| 125 | auto const weigh = QFontDatabase::weight(aFamily, aStyle); |
| 126 | auto const italic = QFontDatabase::italic(aFamily, aStyle); |
| 127 | auto const aPointList = QFontDatabase::pointSizes(aFamily, aStyle); |
| 128 | #else |
| 129 | QFontDatabase aFDB; |
| 130 | auto const isFixedPitch = aFDB.isFixedPitch(aFamily, aStyle); |
| 131 | auto const weigh = aFDB.weight(aFamily, aStyle); |
| 132 | auto const italic = aFDB.italic(aFamily, aStyle); |
| 133 | auto const aPointList = aFDB.pointSizes(aFamily, aStyle); |
| 134 | #endif |
| 135 | |
| 136 | FontAttributes aFA; |
| 137 | |
| 138 | aFA.SetFamilyName(toOUString(aFamily)); |
| 139 | aFA.SetStyleName(toOUString(aStyle)); |
| 140 | aFA.SetPitch(isFixedPitch ? PITCH_FIXED : PITCH_VARIABLE); |
| 141 | aFA.SetWeight(QtFontFace::toFontWeight(weigh)); |
| 142 | aFA.SetItalic(italic ? ITALIC_NORMAL : ITALIC_NONE); |
| 143 | |
| 144 | int nPointSize = 0; |
| 145 | if (!aPointList.empty()) |
| 146 | nPointSize = aPointList[0]; |
| 147 | |
| 148 | return new QtFontFace(aFA, aFamily + "," + aStyle + "," + QString::number(nPointSize), |
| 149 | FontIdType::FontDB); |
| 150 | } |
| 151 | |
| 152 | QtFontFace::QtFontFace(const FontAttributes& rFA, QString aFontID, const FontIdType eFontIdType) |
| 153 | : PhysicalFontFace(rFA) |
| 154 | , m_aFontId(std::move(aFontID)) |
| 155 | , m_eFontIdType(eFontIdType) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | sal_IntPtr QtFontFace::GetFontId() const { return reinterpret_cast<sal_IntPtr>(&m_aFontId); } |
| 160 | |
| 161 | QFont QtFontFace::CreateFont() const |
| 162 | { |
| 163 | QFont aFont; |
| 164 | switch (m_eFontIdType) |
| 165 | { |
| 166 | case FontDB: |
| 167 | { |
| 168 | QStringList aStrList = m_aFontId.split(","); |
| 169 | if (3 == aStrList.size()) |
| 170 | { |
| 171 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 172 | aFont = QFontDatabase::font(aStrList[0], aStrList[1], aStrList[2].toInt()); |
| 173 | #else |
| 174 | QFontDatabase aFDB; |
| 175 | aFont = aFDB.font(aStrList[0], aStrList[1], aStrList[2].toInt()); |
| 176 | #endif |
| 177 | } |
| 178 | else |
| 179 | SAL_WARN("vcl.qt", "Invalid QFontDatabase font ID " << m_aFontId); |
| 180 | break; |
| 181 | } |
| 182 | case Font: |
| 183 | bool bRet = aFont.fromString(m_aFontId); |
| 184 | SAL_WARN_IF(!bRet, "vcl.qt", "Failed to create QFont from ID: " << m_aFontId); |
| 185 | Q_UNUSED(bRet); |
| 186 | break; |
| 187 | } |
| 188 | return aFont; |
| 189 | } |
| 190 | |
| 191 | rtl::Reference<LogicalFontInstance> |
| 192 | QtFontFace::CreateFontInstance(const vcl::font::FontSelectPattern& rFSD) const |
| 193 | { |
| 194 | return new QtFont(*this, rFSD); |
| 195 | } |
| 196 | |
| 197 | hb_blob_t* QtFontFace::GetHbTable(hb_tag_t nTag) const |
| 198 | { |
| 199 | char pTagName[5] = { '\0' }; |
| 200 | hb_tag_to_string(nTag, pTagName); |
| 201 | |
| 202 | QFont aFont = CreateFont(); |
| 203 | QRawFont aRawFont(QRawFont::fromFont(aFont)); |
| 204 | QByteArray aTable = aRawFont.fontTable(pTagName); |
| 205 | const sal_uInt32 nLength = aTable.size(); |
| 206 | |
| 207 | hb_blob_t* pBlob = nullptr; |
| 208 | if (nLength > 0) |
| 209 | pBlob = hb_blob_create(aTable.data(), nLength, HB_MEMORY_MODE_DUPLICATE, nullptr, nullptr); |
| 210 | return pBlob; |
| 211 | } |
| 212 | |
| 213 | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |