ResInsight/ApplicationLibCode/Application/RiaFontCache.cpp

125 lines
4.8 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaFontCache.h"
#include "RiaGuiApplication.h"
#include "cafFixedAtlasFont.h"
#include <QDesktopWidget>
#include <cmath>
2020-05-09 04:23:58 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::FixedAtlasFont::FontSize mapToAtlasFontSize( int pointSize )
{
2020-06-05 05:59:22 -05:00
if ( pointSize >= 6 && pointSize < 8 )
return caf::FixedAtlasFont::POINT_SIZE_6;
else if ( pointSize >= 8 && pointSize < 10 )
2020-05-09 04:23:58 -05:00
return caf::FixedAtlasFont::POINT_SIZE_8;
else if ( pointSize >= 10 && pointSize < 12 )
return caf::FixedAtlasFont::POINT_SIZE_10;
else if ( pointSize >= 12 && pointSize < 14 )
return caf::FixedAtlasFont::POINT_SIZE_12;
else if ( pointSize >= 14 && pointSize < 16 )
return caf::FixedAtlasFont::POINT_SIZE_14;
else if ( pointSize >= 16 && pointSize < 20 )
return caf::FixedAtlasFont::POINT_SIZE_16;
else if ( pointSize >= 20 && pointSize < 28 )
return caf::FixedAtlasFont::POINT_SIZE_24;
2020-05-09 04:23:58 -05:00
return caf::FixedAtlasFont::POINT_SIZE_32;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2020-05-09 04:23:58 -05:00
std::map<caf::FixedAtlasFont::FontSize, cvf::ref<caf::FixedAtlasFont>> RiaFontCache::ms_fonts;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2020-05-09 04:23:58 -05:00
cvf::ref<caf::FixedAtlasFont> RiaFontCache::getFont( FontSize pointFontSize )
{
int pointSize = caf::FontTools::absolutePointSize( pointFontSize );
return getFont( pointSize );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2020-05-09 04:23:58 -05:00
cvf::ref<caf::FixedAtlasFont> RiaFontCache::getFont( int pointSize )
{
2020-05-09 04:23:58 -05:00
int currentDPI = 96;
if ( RiaGuiApplication::isRunning() )
{
2020-05-09 04:23:58 -05:00
currentDPI = RiaGuiApplication::desktop()->logicalDpiX();
}
2020-05-09 04:23:58 -05:00
// the Fixed Atlas Fonts appear to be assuming a DPI of 96, so we need scaling.
double scaling = currentDPI / 96.0;
int scaledSize = scaling * pointSize;
auto atlasFontSize = mapToAtlasFontSize( scaledSize );
auto existing_it = ms_fonts.find( atlasFontSize );
if ( existing_it == ms_fonts.end() )
{
auto newFont = new caf::FixedAtlasFont( atlasFontSize );
bool inserted = false;
std::tie( existing_it, inserted ) = ms_fonts.insert( std::make_pair( atlasFontSize, newFont ) );
CAF_ASSERT( inserted );
}
return existing_it->second;
}
//--------------------------------------------------------------------------------------------------
2020-01-07 05:03:40 -06:00
/// In the 2019 releases the font size was stored as an enum value rather than actual size
/// Use this method for legacy conversion
//--------------------------------------------------------------------------------------------------
2020-01-07 05:03:40 -06:00
RiaFontCache::FontSize RiaFontCache::legacyEnumToPointSize( int enumValue )
{
2020-01-07 05:03:40 -06:00
switch ( enumValue )
{
2020-01-07 05:03:40 -06:00
case 0:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_8;
2020-01-07 05:03:40 -06:00
case 1:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_10;
2020-01-07 05:03:40 -06:00
case 2:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_12;
2020-01-07 05:03:40 -06:00
case 3:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_14;
2020-01-07 05:03:40 -06:00
case 4:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_16;
2020-01-07 05:03:40 -06:00
case 5:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_24;
2020-01-07 05:03:40 -06:00
case 6:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_32;
default:
2020-05-09 04:23:58 -05:00
return FontSize::FONT_SIZE_8;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFontCache::clear()
{
ms_fonts.clear();
}