File indexing completed on 2024-04-28 05:32:10

0001 /*
0002     this file is part of the oxygen gtk engine
0003     SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "oxygenfontinfo.h"
0009 
0010 #include <sstream>
0011 #include <vector>
0012 
0013 namespace Oxygen
0014 {
0015 
0016     //_____________________________________________
0017     std::string FontInfo::toString( bool addQuotes ) const
0018     {
0019         std::ostringstream out;
0020 
0021         if( addQuotes ) out << "\"";
0022 
0023         out << family() << " " << weightString() << " ";
0024         if( _italic ) out << italicString() << " ";
0025         out << size() ;
0026 
0027         if( addQuotes ) out << "\"";
0028 
0029         return out.str();
0030     }
0031 
0032     //_____________________________________________
0033     FontInfo FontInfo::fromKdeOption( std::string value )
0034     {
0035 
0036         FontInfo out;
0037         if( value.empty() ) return out;
0038 
0039         // split strings using "," as a separator
0040         size_t position = 0;
0041         std::vector<std::string> values;
0042         while( ( position = value.find( ',' ) ) != std::string::npos )
0043         {
0044             values.push_back( value.substr( 0, position ) );
0045             value = value.substr( position+1 );
0046         }
0047 
0048         if( !value.empty() ) values.push_back( value );
0049         for( unsigned int index = 0; index < values.size(); index++ )
0050         {
0051 
0052             if( index == 0 ) {
0053 
0054                 out._family = values[index];
0055                 continue;
0056 
0057             }
0058 
0059             std::istringstream in( values[index] );
0060             if( index == 1 )
0061             {
0062 
0063                 double size(0);
0064                 if( in >> size ) out._size = size;
0065                 continue;
0066 
0067             } else if( index == 4 ) {
0068 
0069                 int weight;
0070                 if( in >> weight )
0071                 {
0072                     if( weight < Normal ) out._weight = Light;
0073                     else if( weight < DemiBold ) out._weight = Normal;
0074                     else if( weight < Bold ) out._weight = DemiBold;
0075                     else if( weight < Black ) out._weight = Bold;
0076                     else out._weight = Black;
0077 
0078                 }
0079 
0080                 continue;
0081 
0082             } else if( index == 5 ) {
0083 
0084                 bool italic;
0085                 if( in >> italic ) out._italic = italic;
0086                 continue;
0087 
0088             } else if( index == 8 ) {
0089 
0090                 bool fixed;
0091                 if( in >> fixed ) out._fixed = fixed;
0092                 continue;
0093 
0094            }
0095 
0096         }
0097 
0098         return out;
0099 
0100     }
0101 
0102     //_____________________________________________
0103     std::string FontInfo::weightString( void ) const
0104     {
0105 
0106         switch( _weight )
0107         {
0108             case Light: return "light";
0109             default:
0110             case Normal: return "";
0111             case DemiBold: return "demibold";
0112             case Bold: return "bold";
0113             case Black: return "black";
0114         }
0115 
0116     }
0117 
0118     //_____________________________________________
0119     std::string FontInfo::italicString( void ) const
0120     { return _italic ? "Italic" : ""; }
0121 
0122 }