custom icon generate

This commit is contained in:
Xiao YiFang 2022-07-31 16:17:57 +08:00
parent 1a2e648c40
commit a8817242bf
7 changed files with 81 additions and 9 deletions

View file

@ -18,8 +18,6 @@
#include <QCryptographicHash>
#include <QDateTime>
#include "fsencoding.hh"
#include "langcoder.hh"
#include <QImage>
#include <QPainter>
#include <QRegularExpression>
@ -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() )

View file

@ -16,6 +16,7 @@
#include "langcoder.hh"
#include "config.hh"
#include "utils.hh"
#include <QString>
/// 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() );

View file

@ -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,

BIN
icons/mdict-bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

3
mdx.cc
View file

@ -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" );

View file

@ -101,5 +101,6 @@
<file>icons/closetab-hover.png</file>
<file>icons/1downarrow.svg</file>
<file>icons/wizard-selected.svg</file>
<file>icons/mdict-bg.png</file>
</qresource>
</RCC>

View file

@ -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
*/