From b8f2e677b094dfe4e09f7971ef01316f6b3c21a7 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 31 Mar 2024 13:45:04 +0300 Subject: fixes #4320 (NewStory: problem with small images' preview) --- plugins/NewStory/src/history_control.h | 3 ++ plugins/NewStory/src/main.cpp | 2 ++ plugins/NewStory/src/stdafx.h | 4 +++ plugins/NewStory/src/templates.cpp | 16 +++++++--- plugins/NewStory/src/webpage.cpp | 54 +++++++++++++++++++--------------- 5 files changed, 52 insertions(+), 27 deletions(-) (limited to 'plugins') diff --git a/plugins/NewStory/src/history_control.h b/plugins/NewStory/src/history_control.h index 038a3af366..0a679ff00b 100644 --- a/plugins/NewStory/src/history_control.h +++ b/plugins/NewStory/src/history_control.h @@ -85,6 +85,9 @@ public: ~NSWebPage(); COLORREF clText = -1, clBack = -1; + + Bitmap* find_image(const wchar_t *pwszUrl); + Bitmap* load_image(const wchar_t *pwszUrl); }; struct NewstoryListData : public MZeroedObject diff --git a/plugins/NewStory/src/main.cpp b/plugins/NewStory/src/main.cpp index 6917eb31ff..a74019cffb 100644 --- a/plugins/NewStory/src/main.cpp +++ b/plugins/NewStory/src/main.cpp @@ -18,6 +18,8 @@ along with this program. If not, see . #include "stdafx.h" +#pragma comment(lib, "gdiplus.lib") + CMPlugin g_plugin; CMOption g_bOptGrouping(MODULENAME, "MessageGrouping", false); diff --git a/plugins/NewStory/src/stdafx.h b/plugins/NewStory/src/stdafx.h index f48693cc29..f89220c3ca 100644 --- a/plugins/NewStory/src/stdafx.h +++ b/plugins/NewStory/src/stdafx.h @@ -31,6 +31,10 @@ along with this program. If not, see . #include #include +#undef Translate +#include +using namespace Gdiplus; + #include #include diff --git a/plugins/NewStory/src/templates.cpp b/plugins/NewStory/src/templates.cpp index 809cba555a..900b3b3557 100644 --- a/plugins/NewStory/src/templates.cpp +++ b/plugins/NewStory/src/templates.cpp @@ -45,7 +45,7 @@ static wchar_t* font2html(LOGFONTA &lf, wchar_t *dest) return dest; } -static void AppendString(CMStringW &buf, const wchar_t *p) +static void AppendString(CMStringW &buf, const wchar_t *p, ItemData *pItem) { bool wasSpace = false; @@ -122,7 +122,15 @@ static void AppendString(CMStringW &buf, const wchar_t *p) if (auto *p2 = wcsstr(p1, L"[/img]")) { CMStringW wszDescr(p1, int(p2 - p1)); - buf.AppendFormat(L"\"%s\"/
", wszUrl.c_str(), wszDescr.c_str(), wszDescr.c_str()); + + int iWidth = 300; + pItem->pOwner->webPage.load_image(wszUrl); + if (Bitmap *pImage = pItem->pOwner->webPage.find_image(wszUrl)) + if (pImage->GetWidth() < 300) + iWidth = pImage->GetWidth(); + + buf.AppendFormat(L"\"%s\"/
", + iWidth, wszUrl.c_str(), wszDescr.c_str(), wszDescr.c_str()); p = p2 + 5; } } @@ -168,7 +176,7 @@ CMStringW ItemData::formatHtml(const wchar_t *pwszStr) if (qtext) { str.Append(L"
"); - AppendString(str, qtext); + AppendString(str, qtext, this); str.Append(L"
\n"); } @@ -186,7 +194,7 @@ CMStringW ItemData::formatHtml(const wchar_t *pwszStr) CMStringW szBody(wszOrigText); UrlAutodetect(szBody); - AppendString(str, szBody); + AppendString(str, szBody, this); if (spRes) { int iOffset = 0; for (int i = 0; i < (int)sp.numSmileys; i++) { diff --git a/plugins/NewStory/src/webpage.cpp b/plugins/NewStory/src/webpage.cpp index a2f6bf9043..7ffa2795da 100644 --- a/plugins/NewStory/src/webpage.cpp +++ b/plugins/NewStory/src/webpage.cpp @@ -18,12 +18,6 @@ along with this program. If not, see . #include "stdafx.h" -#undef Translate -#include - -#pragma comment(lib, "gdiplus.lib") -using namespace Gdiplus; - ///////////////////////////////////////////////////////////////////////////////////////// // Litehtml interface @@ -293,6 +287,18 @@ void NSWebPage::make_url_utf8(const char *url, const char *basepath, std::wstrin make_url(Utf2T(url), Utf2T(basepath), out); } +Bitmap* NSWebPage::load_image(const wchar_t *pwszUrl) +{ + mir_cslockfull lck(m_csImages); + auto img = m_images.find(pwszUrl); + if (img != m_images.end() && img->second) + return (Bitmap *)img->second; + + uint_ptr newImg = get_image(pwszUrl, false); + add_image(pwszUrl, newImg); + return (Bitmap *)newImg; +} + void NSWebPage::load_image(const char *src, const char *baseurl, bool redraw_on_ready) { std::wstring url; @@ -313,19 +319,27 @@ void NSWebPage::add_image(LPCWSTR url, uint_ptr img) m_images[url] = img; } +Bitmap* NSWebPage::find_image(const wchar_t *pwszUrl) +{ + mir_cslock lck(m_csImages); + auto img = m_images.find(pwszUrl); + if (img != m_images.end() && img->second) + return (Bitmap *)img->second; + + return nullptr; +} + void NSWebPage::get_image_size(const char *src, const char *baseurl, size &sz) { std::wstring url; make_url_utf8(src, baseurl, url); - sz.width = 0; - sz.height = 0; - mir_cslock lck(m_csImages); images_map::iterator img = m_images.find(url); - if (img != m_images.end() && img->second) { + if (img != m_images.end() && img->second) get_img_size(img->second, sz); - } + else + sz.width = sz.height = 0; } void NSWebPage::clear_images() @@ -346,9 +360,8 @@ void NSWebPage::set_clip(const position &pos, const border_radiuses &) void NSWebPage::del_clip() { - if (!m_clips.empty()) { + if (!m_clips.empty()) m_clips.pop_back(); - } } void NSWebPage::apply_clip(HDC hdc) @@ -486,17 +499,12 @@ void NSWebPage::draw_image(uint_ptr _hdc, const background_layer &bg, const std: if (src.empty() || (!bg.clip_box.width && !bg.clip_box.height)) return; - Bitmap *bgbmp; std::wstring url; make_url_utf8(src.c_str(), base_url.c_str(), url); - { - mir_cslock lck(m_csImages); - images_map::iterator img = m_images.find(url); - if (img != m_images.end() && img->second) - bgbmp = (Bitmap *)img->second; - else - return; - } + + Bitmap *bgbmp = find_image(url.c_str()); + if (!bgbmp) + return; Graphics graphics((HDC)_hdc); graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor); @@ -506,7 +514,7 @@ void NSWebPage::draw_image(uint_ptr _hdc, const background_layer &bg, const std: graphics.SetClip(®); Bitmap *scaled_img = nullptr; - if (bg.origin_box.width != bgbmp->GetWidth() || bg.origin_box.height != bgbmp->GetHeight()) { + if (bg.origin_box.width != (int)bgbmp->GetWidth() || bg.origin_box.height != (int)bgbmp->GetHeight()) { scaled_img = new Bitmap(bg.origin_box.width, bg.origin_box.height); Graphics gr(scaled_img); gr.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality); -- cgit v1.2.3