diff --git a/src/gui/text/freetype/qfontengine_ft.cpp b/src/gui/text/freetype/qfontengine_ft.cpp index 028158fe4b..e00f2c240a 100644 --- a/src/gui/text/freetype/qfontengine_ft.cpp +++ b/src/gui/text/freetype/qfontengine_ft.cpp @@ -899,9 +899,83 @@ bool QFontEngineFT::init(FaceId faceId, bool antialias, GlyphFormat format, stemDarkeningDriver = false; } #endif + // This was copied from QFreeTypeFontDatabase::addTTFile(). Maybe it can be pulled out into a function that's used in both places. + // TODO: Keep in mind that the logic around emboldening and obliquening isn't taken into account yet + QFont::Weight weight = QFont::Normal; - // TODO: Set fontDef.weight, fontDef.style, fontDef.stretch, fontDef.fixedPitch, etc. to fix font matching. - // Could be done analogously to QFreeTypeFontDatabase::addTTFile(). + QFont::Style style = QFont::StyleNormal; + if (face->style_flags & FT_STYLE_FLAG_ITALIC) + style = QFont::StyleItalic; + + if (face->style_flags & FT_STYLE_FLAG_BOLD) + weight = QFont::Bold; + + bool fixedPitch = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH); + + QFont::Stretch stretch = QFont::Unstretched; + TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2); + if (os2) { + // TODO: I removed unicode ranges and writing systems. Maybe add it back. + + if (os2->usWeightClass) { + weight = static_cast(os2->usWeightClass); + } else if (os2->panose[2]) { + int w = os2->panose[2]; + if (w <= 1) + weight = QFont::Thin; + else if (w <= 2) + weight = QFont::ExtraLight; + else if (w <= 3) + weight = QFont::Light; + else if (w <= 5) + weight = QFont::Normal; + else if (w <= 6) + weight = QFont::Medium; + else if (w <= 7) + weight = QFont::DemiBold; + else if (w <= 8) + weight = QFont::Bold; + else if (w <= 9) + weight = QFont::ExtraBold; + else if (w <= 10) + weight = QFont::Black; + } + + switch (os2->usWidthClass) { + case 1: + stretch = QFont::UltraCondensed; + break; + case 2: + stretch = QFont::ExtraCondensed; + break; + case 3: + stretch = QFont::Condensed; + break; + case 4: + stretch = QFont::SemiCondensed; + break; + case 5: + stretch = QFont::Unstretched; + break; + case 6: + stretch = QFont::SemiExpanded; + break; + case 7: + stretch = QFont::Expanded; + break; + case 8: + stretch = QFont::ExtraExpanded; + break; + case 9: + stretch = QFont::UltraExpanded; + break; + } + } + + fontDef.weight = weight; + fontDef.style = style; + fontDef.stretch = stretch; + fontDef.fixedPitch = fixedPitch; fontDef.styleName = QString::fromUtf8(face->style_name); if (!freetype->hbFace) {