Skip to content

Commit

Permalink
fix: dsl max width image
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Oct 8, 2023
1 parent 3ab2d10 commit 4474330
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/article_maker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ string ArticleMaker::makeEmptyPageHtml() const
sptr< Dictionary::DataRequest > ArticleMaker::makePicturePage( string const & url ) const
{
string const result = makeHtmlHeader( tr( "(picture)" ), QString(), true )
+ R"lit(<a href="javascript: if(history.length>2) history.go(-1)">)lit" + R"(<img src=")" + url + R"(" /></a>)"
+ R"(<img src=")" + url + R"(" />)"
+ "</body></html>";

sptr< Dictionary::DataRequestInstant > r = std::make_shared< Dictionary::DataRequestInstant >( true );
Expand Down
59 changes: 5 additions & 54 deletions src/dict/dsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -840,61 +840,12 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );

vector< char > imgdata;
bool resize = false;
string maxWidthStyle = " style=\"max-width:100%;\" ";

try {
File::loadFromFile( n, imgdata );
}
catch ( File::exCantOpen & ) {
try {
n = resourceDir2 + filename;
File::loadFromFile( n, imgdata );
}
catch ( File::exCantOpen & ) {
try {
n = getContainingFolder().toStdString() + Utils::Fs::separator() + filename;
File::loadFromFile( n, imgdata );
}
catch ( File::exCantOpen & ) {
// Try reading from zip file
if ( resourceZip.isOpen() ) {
QMutexLocker _( &resourceZipMutex );
resourceZip.loadFile( Utf8::decode( filename ), imgdata );
}
}
}
}
catch ( ... ) {
}

if ( !imgdata.empty() ) {
if ( Filetype::isNameOfSvg( filename ) ) {
// We don't need to render svg file now

QSvgRenderer svg;
svg.load( QByteArray::fromRawData( imgdata.data(), imgdata.size() ) );
if ( svg.isValid() ) {
QSize imgsize = svg.defaultSize();
resize = maxPictureWidth > 0 && imgsize.width() > maxPictureWidth;
}
}
else {
QImage img = QImage::fromData( (unsigned char *)&imgdata.front(), imgdata.size() );

resize = maxPictureWidth > 0 && img.width() > maxPictureWidth;
}
}

if ( resize ) {
string link( url.toEncoded().data() );
link.replace( 0, 4, "gdpicture" );
result += string( "<a href=\"" ) + link + "\">" + "<img src=\"" + url.toEncoded().data() + "\" alt=\""
+ Html::escape( filename ) + "\"" + "width=\"" + QString::number( maxPictureWidth ).toStdString() + "\"/>"
+ "</a>";
}
else
result += string( "<img src=\"" ) + url.toEncoded().data() + "\" alt=\"" + Html::escape( filename ) + "\"/>";
string link( url.toEncoded().data() );
link.replace( 0, 4, "gdpicture" );
result += string( "<a href=\"" ) + link + "\">" + "<img src=\"" + url.toEncoded().data() + "\" " + maxWidthStyle
+ " alt=\"" + Html::escape( filename ) + "\"/>" + "</a>";
}
else if ( Filetype::isNameOfVideo( filename ) ) {
QUrl url;
Expand Down
9 changes: 5 additions & 4 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
GlobalBroadcaster::instance()->setPreference( &cfg.preferences );

localSchemeHandler = new LocalSchemeHandler( articleNetMgr, this );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "gdlookup", localSchemeHandler );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "bword", localSchemeHandler );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "entry", localSchemeHandler );
QStringList htmlScheme = { "gdlookup","bword", "entry","gdpicture"};
for ( const auto & localScheme : htmlScheme ) {
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( localScheme.toLatin1(), localSchemeHandler );
}

iframeSchemeHandler = new IframeSchemeHandler( this );
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( "ifr", iframeSchemeHandler );

QStringList localSchemes = { "gdau", "gico", "qrcx", "bres", "gdprg", "gdvideo", "gdpicture", "gdtts" };
QStringList localSchemes = { "gdau", "gico", "qrcx", "bres", "gdprg", "gdvideo", "gdtts" };
resourceSchemeHandler = new ResourceSchemeHandler( articleNetMgr, this );
for ( const auto & localScheme : localSchemes ) {
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler( localScheme.toLatin1(), resourceSchemeHandler );
Expand Down

0 comments on commit 4474330

Please sign in to comment.