diff --git a/dictionary.cc b/dictionary.cc index 4bc4ab8b..0a3aa572 100644 --- a/dictionary.cc +++ b/dictionary.cc @@ -18,8 +18,6 @@ #include #include #include "fsencoding.hh" -#include "langcoder.hh" - #include #include #include @@ -275,6 +273,71 @@ bool Class::loadIconFromFile( QString const & _filename, bool isFullName ) return false; } +bool Class::loadIconFromText( QString const & text ) +{ + if( text.isEmpty() ) + return false; + QImage img( ":/icons/mdict-bg.png" ); + + if ( !img.isNull() ) + { + int iconSize = 48; + //some icon is very large ,will crash the application. + img = img.scaledToWidth( iconSize ); + QImage result( iconSize, iconSize, QImage::Format_ARGB32 ); + + int max = img.width() > img.height() ? img.width() : img.height(); + + QPainter painter( &result ); + painter.setRenderHint(QPainter::RenderHint::Antialiasing); + painter.drawImage( QPoint( img.width() == max ? 0 : ( max - img.width() ) / 2, + img.height() == max ? 0 : ( max - img.height() ) / 2 ), + img ); + QFont font = painter.font(); + //the text should be a little smaller than the icon + font.setPixelSize( iconSize * 0.6 ); + font.setWeight( QFont::Black ); + painter.setFont( font ); + + const QRect rectangle = QRect( 0, 0, iconSize, iconSize ); + + //select a single char. + auto abbrName = getAbbrName( text ); + + painter.drawText( rectangle, Qt::AlignCenter, abbrName); + + painter.end(); + + dictionaryNativeIcon = dictionaryIcon = QIcon( QPixmap::fromImage( result ) ); + + return !dictionaryIcon.isNull(); + } + return false; +} + +QString Class::getAbbrName( QString const & text ) +{ + if(text.isEmpty()) + return QString(); + //remove whitespace + QString simplified = text; + simplified.remove(QRegularExpression("\\s")); + int index = qHash( simplified ) % simplified.size(); + + QString abbrName; + if( !Utils::isCJKChar( simplified.at( index ).unicode() ) ) + { + // take two chars. + abbrName = simplified.mid( index, 2 ); + } + else + { + abbrName = simplified.mid( index, 1 ); + } + + return abbrName; +} + void Class::isolateCSS( QString & css, QString const & wrapperSelector ) { if( css.isEmpty() ) diff --git a/dictionary.hh b/dictionary.hh index 08b9ff5a..cf984210 100644 --- a/dictionary.hh +++ b/dictionary.hh @@ -16,6 +16,7 @@ #include "langcoder.hh" #include "config.hh" #include "utils.hh" +#include /// Abstract dictionary-related stuff namespace Dictionary { @@ -275,6 +276,9 @@ protected: // Load icon from filename directly if isFullName == true // else treat filename as name without extension bool loadIconFromFile( QString const & filename, bool isFullName = false ); + bool loadIconFromText( QString const & text ); + + QString getAbbrName( QString const & text ); /// Make css content usable only for articles from this dictionary void isolateCSS( QString & css, QString const & wrapperSelector = QString() ); diff --git a/ftshelpers.cc b/ftshelpers.cc index db6e6b13..76eb07ce 100644 --- a/ftshelpers.cc +++ b/ftshelpers.cc @@ -468,12 +468,7 @@ void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancell bool isCJKChar( ushort ch ) { - if( ( ch >= 0x3400 && ch <= 0x9FFF ) - || ( ch >= 0xF900 && ch <= 0xFAFF ) - || ( ch >= 0xD800 && ch <= 0xDFFF ) ) - return true; - - return false; + return Utils::isCJKChar(ch); } void FTSResultsRequest::checkArticles( QVector< uint32_t > const & offsets, diff --git a/icons/mdict-bg.png b/icons/mdict-bg.png new file mode 100644 index 00000000..89868894 Binary files /dev/null and b/icons/mdict-bg.png differ diff --git a/mdx.cc b/mdx.cc index 70de1524..c83d4ea4 100644 --- a/mdx.cc +++ b/mdx.cc @@ -900,8 +900,9 @@ void MdxDictionary::loadIcon() noexcept // Remove the extension fileName.chop( 3 ); + QString text = QString::fromStdString( dictionaryName ); - if ( !loadIconFromFile( fileName ) ) + if( !loadIconFromFile( fileName ) && !loadIconFromText( text ) ) { // Use default icons dictionaryIcon = dictionaryNativeIcon = QIcon( ":/icons/mdict.png" ); diff --git a/resources.qrc b/resources.qrc index 77ea67e0..50ae8ec8 100644 --- a/resources.qrc +++ b/resources.qrc @@ -101,5 +101,6 @@ icons/closetab-hover.png icons/1downarrow.svg icons/wizard-selected.svg + icons/mdict-bg.png diff --git a/utils.hh b/utils.hh index a30a144c..28db1830 100644 --- a/utils.hh +++ b/utils.hh @@ -15,7 +15,15 @@ namespace Utils { +inline bool isCJKChar( ushort ch ) +{ + if( ( ch >= 0x3400 && ch <= 0x9FFF ) + || ( ch >= 0xF900 && ch <= 0xFAFF ) + || ( ch >= 0xD800 && ch <= 0xDFFF ) ) + return true; + return false; +} /** * remove right end space */