First round of caf::FontTools

This commit is contained in:
Gaute Lindkvist
2020-05-09 11:23:58 +02:00
parent 0e70cf809c
commit 2fe4372dc2
86 changed files with 1021 additions and 966 deletions

View File

@@ -20,71 +20,70 @@
#include "RiaGuiApplication.h"
#include "cafAppEnum.h"
#include "cafFixedAtlasFont.h"
#include <QDesktopWidget>
#include <cmath>
namespace caf
{
template <>
void RiaFontCache::FontSizeType::setUp()
{
addItem( RiaFontCache::FONT_SIZE_8, "8", "8" );
addItem( RiaFontCache::FONT_SIZE_10, "10", "10" );
addItem( RiaFontCache::FONT_SIZE_12, "12", "12" );
addItem( RiaFontCache::FONT_SIZE_14, "14", "14" );
addItem( RiaFontCache::FONT_SIZE_16, "16", "16" );
addItem( RiaFontCache::FONT_SIZE_24, "24", "24" );
addItem( RiaFontCache::FONT_SIZE_32, "32", "32" );
setDefault( RiaFontCache::FONT_SIZE_8 );
}
} // namespace caf
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::FixedAtlasFont::FontSize mapToAtlasFontSize( RiaFontCache::FontSize fontSize )
caf::FixedAtlasFont::FontSize mapToAtlasFontSize( int pointSize )
{
switch ( fontSize )
{
case RiaFontCache::FONT_SIZE_8:
return caf::FixedAtlasFont::POINT_SIZE_8;
case RiaFontCache::FONT_SIZE_10:
return caf::FixedAtlasFont::POINT_SIZE_10;
case RiaFontCache::FONT_SIZE_12:
return caf::FixedAtlasFont::POINT_SIZE_12;
case RiaFontCache::FONT_SIZE_14:
return caf::FixedAtlasFont::POINT_SIZE_14;
case RiaFontCache::FONT_SIZE_16:
return caf::FixedAtlasFont::POINT_SIZE_16;
case RiaFontCache::FONT_SIZE_24:
return caf::FixedAtlasFont::POINT_SIZE_24;
case RiaFontCache::FONT_SIZE_32:
return caf::FixedAtlasFont::POINT_SIZE_32;
default:
return caf::FixedAtlasFont::POINT_SIZE_16;
}
if ( pointSize < 10 )
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;
return caf::FixedAtlasFont::POINT_SIZE_32;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::map<RiaFontCache::FontSize, cvf::ref<caf::FixedAtlasFont>> RiaFontCache::ms_fonts;
std::map<caf::FixedAtlasFont::FontSize, cvf::ref<caf::FixedAtlasFont>> RiaFontCache::ms_fonts;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<caf::FixedAtlasFont> RiaFontCache::getFont( FontSize size )
cvf::ref<caf::FixedAtlasFont> RiaFontCache::getFont( FontSize pointFontSize )
{
if ( ms_fonts.count( size ) == 0 )
int pointSize = caf::FontTools::absolutePointSize( pointFontSize );
return getFont( pointSize );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<caf::FixedAtlasFont> RiaFontCache::getFont( int pointSize )
{
int currentDPI = 96;
if ( RiaGuiApplication::isRunning() )
{
auto newFont = new caf::FixedAtlasFont( mapToAtlasFontSize( size ) );
ms_fonts.insert( std::make_pair( size, newFont ) );
currentDPI = RiaGuiApplication::desktop()->logicalDpiX();
}
return ms_fonts[size];
// 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;
}
//--------------------------------------------------------------------------------------------------
@@ -96,76 +95,24 @@ RiaFontCache::FontSize RiaFontCache::legacyEnumToPointSize( int enumValue )
switch ( enumValue )
{
case 0:
return FONT_SIZE_8;
return FontSize::FONT_SIZE_8;
case 1:
return FONT_SIZE_10;
return FontSize::FONT_SIZE_10;
case 2:
return FONT_SIZE_12;
return FontSize::FONT_SIZE_12;
case 3:
return FONT_SIZE_14;
return FontSize::FONT_SIZE_14;
case 4:
return FONT_SIZE_16;
return FontSize::FONT_SIZE_16;
case 5:
return FONT_SIZE_24;
return FontSize::FONT_SIZE_24;
case 6:
return FONT_SIZE_32;
return FontSize::FONT_SIZE_32;
default:
return FONT_SIZE_8;
return FontSize::FONT_SIZE_8;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaFontCache::FontSize RiaFontCache::fontSizeEnumFromPointSize( int pointSize )
{
std::vector<FontSize> allValues =
{FONT_SIZE_8, FONT_SIZE_10, FONT_SIZE_12, FONT_SIZE_14, FONT_SIZE_16, FONT_SIZE_24, FONT_SIZE_32};
FontSize closestEnumValue = FONT_SIZE_8;
int closestDiff = std::numeric_limits<int>::max();
for ( FontSize enumValue : allValues )
{
int diff = std::abs( (int)enumValue - pointSize );
if ( diff < closestDiff )
{
closestEnumValue = enumValue;
closestDiff = diff;
}
}
return closestEnumValue;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaFontCache::pointSizeToPixelSize( int pointSize )
{
auto app = RiaGuiApplication::instance();
if ( app )
{
int dpi = app->desktop()->logicalDpiX();
double inches = pointSize / 72.0;
return static_cast<int>( std::ceil( inches * dpi ) );
}
return pointSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaFontCache::pixelSizeToPointSize( int pixelSize )
{
auto app = RiaGuiApplication::instance();
if ( app )
{
int dpi = app->desktop()->logicalDpiX();
double inches = pixelSize / dpi;
return static_cast<int>( std::ceil( inches * 72.0 ) );
}
return pixelSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------