Skip to content

Commit

Permalink
Merge branch 'staged' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Dec 17, 2024
2 parents bed76f3 + b7a27d7 commit 7f62fc8
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/PR-check-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
brew update
- name: Install dependencies
run: |
brew install \
brew install --force --overwrite \
ninja \
opencc \
ffmpeg \
Expand All @@ -68,7 +68,7 @@ jobs:
hunspell \
xapian \
libzim \
qt
qt || true
- name: Install eb
run: |
wget https://github.com/mistydemeo/eb/releases/download/v4.4.3/eb-4.4.3.tar.bz2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/Release-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
brew update
- name: Install dependencies
run: |
brew install \
brew install --force --overwrite \
bzip2 \
create-dmg \
hunspell \
Expand All @@ -42,7 +42,7 @@ jobs:
lzip \
ninja \
opencc \
xapian
xapian || true
- name: Install eb
run: |
git clone https://github.com/xiaoyifang/eb.git
Expand Down
Binary file modified icons/icon32_stardict.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions src/common/dictionary_icon_name.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "dictionary_icon_name.hh"
#include <QMutexLocker>


QString Icons::DictionaryIconName::getIconName( const QString & dictionaryName )
{
if ( dictionaryName.isEmpty() ) {
return {};
}
QMutexLocker _( &_mutex );

auto it = _dictionaryIconNames.contains( dictionaryName );
if ( it ) {
return _dictionaryIconNames.value( dictionaryName );
}
//get the first character of the dictionary name
QString name = dictionaryName.at( 0 ).toUpper();
auto it1 = _iconDictionaryNames.contains( name );
std::vector< QString > vector = {};
if ( it1 ) {
vector = _iconDictionaryNames.value( name );
vector.emplace_back( dictionaryName );
}
else {
vector.emplace_back( dictionaryName );
_iconDictionaryNames.insert( name, vector );
}

name = name + QString::number( vector.size() );
_dictionaryIconNames.insert( dictionaryName, name );
return name;
}
24 changes: 24 additions & 0 deletions src/common/dictionary_icon_name.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string>
#include <QMap>
#include <vector>
#include <mutex>
#include <QString>
#include <QMutex>

namespace Icons {
//use dictionary name's (first character + the order number) to represent the dictionary name in the icon image.
class DictionaryIconName
{
//map icon name to dictionary names;
QMap< QString, std::vector< QString > > _iconDictionaryNames;
//map dictionary name to icon name;
QMap< QString, QString > _dictionaryIconNames;

QMutex _mutex;

public:
QString getIconName( const QString & dictionaryName );
};
} // namespace Icons
3 changes: 1 addition & 2 deletions src/common/folding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ std::u32string applyWhitespaceAndPunctOnly( std::u32string const & in )

bool isWhitespace( char32_t ch )
{
//invisible character should be treated as whitespace as well.
return QChar::isSpace( ch ) || !QChar::isPrint( ch );
return QChar::isSpace( ch );
}

bool isWhitespaceOrPunct( char32_t ch )
Expand Down
18 changes: 18 additions & 0 deletions src/common/globalbroadcaster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@ bool GlobalBroadcaster::existedInWhitelist( QString url ) const
{
return whitelist.contains( url );
}


QString GlobalBroadcaster::getAbbrName( QString const & text )
{
if ( text.isEmpty() ) {
return {};
}
//remove whitespace,number,mark,puncuation,symbol
QString simplified = text;
simplified.remove(
QRegularExpression( R"([\p{Z}\p{N}\p{M}\p{P}\p{S}])", QRegularExpression::UseUnicodePropertiesOption ) );

if ( simplified.isEmpty() ) {
return {};
}

return _icon_names.getIconName( simplified );
}
// namespace global
3 changes: 3 additions & 0 deletions src/common/globalbroadcaster.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config.hh"
#include "pronounceengine.hh"
#include <QCache>
#include "dictionary_icon_name.hh"

struct ActiveDictIds
{
Expand All @@ -25,6 +26,7 @@ class GlobalBroadcaster: public QObject

Config::Preferences * preference;
QSet< QString > whitelist;
Icons::DictionaryIconName _icon_names;

public:
void setPreference( Config::Preferences * _pre );
Expand All @@ -40,6 +42,7 @@ public:
QMap< QString, QSet< QString > > folderFavoritesMap;
QMap< unsigned, QString > groupFolderMap;
PronounceEngine pronounce_engine;
QString getAbbrName( QString const & text );
signals:
void dictionaryChanges( ActiveDictIds ad );
void dictionaryClear( ActiveDictIds ad );
Expand Down
4 changes: 2 additions & 2 deletions src/common/globalregex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ const static QRegularExpression accentMark( R"(\p{M})", QRegularExpression::UseU
//contain unicode space mark,invisible, and punctuation
const static QRegularExpression markPuncSpace( R"([\p{M}\p{Z}\p{C}\p{P}])",
QRegularExpression::UseUnicodePropertiesOption );
//contain unicode space and mark.invisible
const static QRegularExpression markSpace( R"([\p{M}\p{Z}\p{C}])", QRegularExpression::UseUnicodePropertiesOption );
//contain unicode space and mark.
const static QRegularExpression markSpace( R"([\p{M}\p{Z}])", QRegularExpression::UseUnicodePropertiesOption );

const static QRegularExpression whiteSpace( "\\s+" );

Expand Down
13 changes: 13 additions & 0 deletions src/common/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ inline QString rstrip( const QString & str )
return {};
}

inline uint32_t leadingSpaceCount( const QString & str )
{
for ( int i = 0; i < str.size(); i++ ) {
if ( str.at( i ).isSpace() ) {
continue;
}
else {
return i;
}
}
return 0;
}

std::string c_string( const QString & str );
bool endsWithIgnoreCase( QByteArrayView str, QByteArrayView extension );
/**
Expand Down
69 changes: 39 additions & 30 deletions src/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QRegularExpression>
#include "utils.hh"
#include "zipfile.hh"
#include <array>

namespace Dictionary {

Expand Down Expand Up @@ -291,7 +292,7 @@ bool Class::loadIconFromFile( QString const & _filename, bool isFullName )
return false;
}

bool Class::loadIconFromText( QString iconUrl, QString const & text )
bool Class::loadIconFromText( const QString & iconUrl, QString const & text )
{
if ( text.isEmpty() ) {
return false;
Expand All @@ -308,7 +309,7 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text )
painter.setCompositionMode( QPainter::CompositionMode_SourceAtop );

QFont font = painter.font();
//the text should be a little smaller than the icon
//the orderNum should be a little smaller than the icon
font.setPixelSize( iconSize * 0.6 );
font.setWeight( QFont::Bold );
painter.setFont( font );
Expand All @@ -318,8 +319,21 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text )
//select a single char.
auto abbrName = getAbbrName( text );

painter.setPen( QColor( 4, 57, 108, 200 ) );
painter.drawText( rectangle, Qt::AlignCenter, abbrName );
painter.setPen( intToFixedColor( qHash( abbrName ) ) );

// Draw first character
painter.drawText( rectangle, Qt::AlignCenter, abbrName.at( 0 ) );

//the orderNum should be a little smaller than the icon
font.setPixelSize( iconSize * 0.4 );
QFontMetrics fm1( font );
const QString & orderNum = abbrName.mid( 1 );
int orderNumberWidth = fm1.horizontalAdvance( orderNum );

painter.setFont( font );
painter.drawText( rectangle.x() + rectangle.width() - orderNumberWidth * 1.2,
rectangle.y() + rectangle.height(),
orderNum );

painter.end();

Expand All @@ -330,35 +344,30 @@ bool Class::loadIconFromText( QString iconUrl, QString const & text )
return false;
}

QString Class::getAbbrName( QString const & text )
QColor Class::intToFixedColor( int index )
{
if ( text.isEmpty() ) {
return {};
}
//remove whitespace,number,mark,puncuation,symbol
QString simplified = text;
simplified.remove(
QRegularExpression( R"([\p{Z}\p{N}\p{M}\p{P}\p{S}])", QRegularExpression::UseUnicodePropertiesOption ) );
// Predefined list of colors
static const std::array colors = {
QColor( 255, 0, 0, 200 ), // Red
QColor( 4, 57, 108, 200 ), //Custom
QColor( 0, 255, 0, 200 ), // Green
QColor( 0, 0, 255, 200 ), // Blue
QColor( 255, 255, 0, 200 ), // Yellow
QColor( 0, 255, 255, 200 ), // Cyan
QColor( 255, 0, 255, 200 ), // Magenta
QColor( 192, 192, 192, 200 ), // Gray
QColor( 255, 165, 0, 200 ), // Orange
QColor( 128, 0, 128, 200 ), // Violet
QColor( 128, 128, 0, 200 ) // Olive
};

if ( simplified.isEmpty() ) {
return {};
}
int index = qHash( simplified ) % simplified.size();

QString abbrName;
if ( !Utils::isCJKChar( simplified.at( index ).unicode() ) ) {
// take two chars.
abbrName = simplified.mid( index, 2 );
if ( abbrName.size() == 1 ) {
//make up two characters.
abbrName = abbrName + simplified.at( 0 );
}
}
else {
abbrName = simplified.mid( index, 1 );
}
// Use modulo operation to ensure index is within the range of the color list
return colors[ index % colors.size() ];
}

return abbrName;
QString Class::getAbbrName( QString const & text )
{
return GlobalBroadcaster::instance()->getAbbrName( text );
}

void Class::isolateCSS( QString & css, QString const & wrapperSelector )
Expand Down
6 changes: 3 additions & 3 deletions src/dict/dictionary.hh
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ 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 iconUrl, QString const & text );

QString getAbbrName( QString const & text );
bool loadIconFromText( const QString & iconUrl, QString const & text );

static QString getAbbrName( QString const & text );
static QColor intToFixedColor( int index );
/// Make css content usable only for articles from this dictionary
void isolateCSS( QString & css, QString const & wrapperSelector = QString() );

Expand Down
Loading

0 comments on commit 7f62fc8

Please sign in to comment.