diff options
Diffstat (limited to 'src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm')
-rw-r--r-- | src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm index 49a6049c4b..66baf162d9 100644 --- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm +++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm @@ -112,25 +112,32 @@ bool QCoreTextFontEngine::ct_getSfntTable(void *user_data, uint tag, uchar *buff QFont::Weight QCoreTextFontEngine::qtWeightFromCFWeight(float value) { - if (value >= kCTFontWeightBlack) - return QFont::Black; - if (value >= kCTFontWeightHeavy) - return QFont::ExtraBold; - if (value >= kCTFontWeightBold) - return QFont::Bold; - if (value >= kCTFontWeightSemibold) - return QFont::DemiBold; - if (value >= kCTFontWeightMedium) - return QFont::Medium; - if (value == kCTFontWeightRegular) - return QFont::Normal; - if (value <= kCTFontWeightUltraLight) - return QFont::Thin; - if (value <= kCTFontWeightThin) - return QFont::ExtraLight; - if (value <= kCTFontWeightLight) - return QFont::Light; - return QFont::Normal; +#define COMPARE_WEIGHT_DISTANCE(ct_weight, qt_weight) \ + { \ + float d; \ + if ((d = qAbs(value - ct_weight)) < distance) { \ + distance = d; \ + ret = qt_weight; \ + } \ + } + + float distance = qAbs(value - kCTFontWeightBlack); + QFont::Weight ret = QFont::Black; + + // Compare distance to system weight to find the closest match. + // (Note: Must go from high to low, so that midpoints are rounded up) + COMPARE_WEIGHT_DISTANCE(kCTFontWeightHeavy, QFont::ExtraBold); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightBold, QFont::Bold); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightSemibold, QFont::DemiBold); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightMedium, QFont::Medium); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightRegular, QFont::Normal); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightLight, QFont::Light); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightThin, QFont::ExtraLight); + COMPARE_WEIGHT_DISTANCE(kCTFontWeightUltraLight, QFont::Thin); + +#undef COMPARE_WEIGHT_DISTANCE + + return ret; } static void loadAdvancesForGlyphs(CTFontRef ctfont, |