From 59f9df67537382bd0249c4e617fe472a1bf4087a Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 7 Oct 2024 16:27:54 +0300 Subject: =?UTF-8?q?fixes=20#4695=20(Newstory:=20BBCode=20=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=20[img]=20=D0=BF=D1=80=D0=B8=20=D0=B7=D0=B0=D0=B2=D0=B5?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=BE=20=D0=BD=D0=B5=D0=B2=D0=B5=D1=80=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BC=D0=B0=D0=B5=D1=82=20=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=BE=D0=B5=20=D0=BF=D1=83=D1=81=D1=82=D0=BE=D0=B5?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D0=B2=20=D0=BE=D0=BA=D0=BD?= =?UTF-8?q?=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/NewStory/src/history_control.h | 6 ---- plugins/NewStory/src/main.cpp | 6 ++++ plugins/NewStory/src/resource.h | 1 + plugins/NewStory/src/stdafx.h | 3 ++ plugins/NewStory/src/templates.cpp | 13 +++++---- plugins/NewStory/src/utils.cpp | 31 +++++++++++++++++++++ plugins/NewStory/src/utils.h | 2 ++ plugins/NewStory/src/webpage.cpp | 50 +++++++++------------------------- 8 files changed, 64 insertions(+), 48 deletions(-) (limited to 'plugins/NewStory/src') diff --git a/plugins/NewStory/src/history_control.h b/plugins/NewStory/src/history_control.h index 8b95ed2e1b..3db27132d4 100644 --- a/plugins/NewStory/src/history_control.h +++ b/plugins/NewStory/src/history_control.h @@ -9,8 +9,6 @@ class NSWebPage : public document_container { typedef std::map images_map; - ULONG_PTR m_gdiplusToken; - mir_cs m_csImages; images_map m_images; @@ -23,7 +21,6 @@ class NSWebPage : public document_container std::string resolve_color(const string &color) const; uint_ptr get_image(LPCWSTR url_or_path, bool redraw_on_ready); - void make_url(LPCWSTR url, LPCWSTR basepath, std::wstring &out); void get_client_rect(position &client) const override; void import_css(string &text, const string &url, string &baseurl) override; @@ -34,7 +31,6 @@ class NSWebPage : public document_container void draw_image(uint_ptr hdc, const background_layer &layer, const std::string &url, const std::string &base_url) override; void get_img_size(uint_ptr img, size &sz); - void free_image(uint_ptr img); // document_container members uint_ptr create_font(const char *faceName, int size, int weight, font_style italic, unsigned int decoration, font_metrics *fm) override; @@ -74,8 +70,6 @@ class NSWebPage : public document_container void release_clip(HDC hdc); void set_clip(const position &pos, const border_radiuses &bdr_radius) override; - void make_url_utf8(const char *url, const char *basepath, std::wstring &out); - void clear_images(); static int CALLBACK EnumFontsProc(const LOGFONT *lplf, const TEXTMETRIC *lptm, DWORD dwType, LPARAM lpData); diff --git a/plugins/NewStory/src/main.cpp b/plugins/NewStory/src/main.cpp index 5e8a4c1bb5..3271b9fe48 100644 --- a/plugins/NewStory/src/main.cpp +++ b/plugins/NewStory/src/main.cpp @@ -185,6 +185,10 @@ int CMPlugin::Load() { registerIcon(MODULETITLE, icons); + GdiplusStartupInput gdiplusStartupInput; + GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); + + m_pNoImage = LoadImageFromResource(getInst(), IDI_NO_IMAGE, L"PNG"); LoadOptions(); m_log = RegisterSrmmLog(this, MODULETITLE, _T(MODULENAME), NewStory_Stub); @@ -211,6 +215,8 @@ int CMPlugin::Unload() WindowList_Destroy(g_hNewstoryLogs); WindowList_Destroy(g_hNewstoryWindows); + GdiplusShutdown(m_gdiplusToken); + UnregisterSrmmLog(m_log); UnregisterClass(_T(NEWSTORYLIST_CLASS), g_plugin.getInst()); DestroyFonts(); diff --git a/plugins/NewStory/src/resource.h b/plugins/NewStory/src/resource.h index 2684ee58d0..f6312dcc6a 100644 --- a/plugins/NewStory/src/resource.h +++ b/plugins/NewStory/src/resource.h @@ -21,6 +21,7 @@ #define IDI_MSGOUT 117 #define IDI_SIGNIN 118 #define IDI_OK 119 +#define IDI_NO_IMAGE 120 #define IDI_UNKNOWN 121 #define IDI_FINDPREV 122 #define IDI_FINDNEXT 123 diff --git a/plugins/NewStory/src/stdafx.h b/plugins/NewStory/src/stdafx.h index f89220c3ca..324b17befc 100644 --- a/plugins/NewStory/src/stdafx.h +++ b/plugins/NewStory/src/stdafx.h @@ -107,6 +107,9 @@ struct CMPlugin : public PLUGIN HANDLE m_log; HBRUSH hBackBrush; + Bitmap *m_pNoImage; + ULONG_PTR m_gdiplusToken; + CMOption bOptVScroll, bSortAscending; // thesw options are a copy of static CMOption to keep performance high diff --git a/plugins/NewStory/src/templates.cpp b/plugins/NewStory/src/templates.cpp index bb3c4ecfca..2c9ac5c222 100644 --- a/plugins/NewStory/src/templates.cpp +++ b/plugins/NewStory/src/templates.cpp @@ -48,14 +48,17 @@ static wchar_t* font2html(LOGFONTA &lf, wchar_t *dest) static void AppendImage(CMStringW &buf, const CMStringW &wszUrl, const CMStringW &wszDescr, ItemData *pItem, UINT uMaxHeight = 300) { if (g_plugin.bShowPreview) { - int iHeight = uMaxHeight; pItem->pOwner->webPage.load_image(wszUrl, pItem); - if (Bitmap *pImage = pItem->pOwner->webPage.find_image(wszUrl)) + + int iHeight = uMaxHeight; + if (auto *pImage = pItem->pOwner->webPage.find_image(wszUrl)) { if (pImage->GetHeight() < uMaxHeight) iHeight = pImage->GetHeight(); - - buf.AppendFormat(L"\"%s\"/
", - iHeight, wszUrl.c_str(), wszDescr.c_str(), wszDescr.c_str()); + + buf.AppendFormat(L"\"%s\"/
", + iHeight, wszUrl.c_str(), wszDescr.c_str(), wszDescr.c_str()); + } + else buf.AppendFormat(L"\"%s\"/
", wszUrl.c_str(), wszUrl.c_str(), wszUrl.c_str()); } else buf.AppendFormat(L"%s", wszUrl.c_str(), wszDescr.c_str()); } diff --git a/plugins/NewStory/src/utils.cpp b/plugins/NewStory/src/utils.cpp index a02d6aa393..5f990fa337 100644 --- a/plugins/NewStory/src/utils.cpp +++ b/plugins/NewStory/src/utils.cpp @@ -18,6 +18,37 @@ along with this program. If not, see . #include "stdafx.h" +#include + +Bitmap* LoadImageFromResource(HINSTANCE hInst, int resourceId, const wchar_t *pwszType) +{ + if (HRSRC hrsrc = FindResourceW(hInst, MAKEINTRESOURCE(resourceId), pwszType)) { + if (DWORD dwSize = SizeofResource(hInst, hrsrc)) { + if (HGLOBAL hRes = LoadResource(hInst, hrsrc)) { + void *pImage = LockResource(hRes); + + if (HGLOBAL hGlobal = ::GlobalAlloc(GHND, dwSize)) { + void *pBuffer = ::GlobalLock(hGlobal); + if (pBuffer) { + memcpy(pBuffer, pImage, dwSize); + + CComPtr pStream; + HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream); + if (SUCCEEDED(hr)) + return new Gdiplus::Bitmap(pStream); + } + + GlobalFree(hGlobal); // free memory only if the function fails + } + } + } + } + + return nullptr; +} + +///////////////////////////////////////////////////////////////////////////////////////// + uint32_t toggleBit(uint32_t dw, uint32_t bit) { if (dw & bit) diff --git a/plugins/NewStory/src/utils.h b/plugins/NewStory/src/utils.h index d9fb352546..d6bfb0ad2f 100644 --- a/plugins/NewStory/src/utils.h +++ b/plugins/NewStory/src/utils.h @@ -11,3 +11,5 @@ int GetFontHeight(const LOGFONTA &lf); void UrlAutodetect(CMStringW &str); void RemoveBbcodes(CMStringW &pwszText); + +Bitmap* LoadImageFromResource(HINSTANCE, int, const wchar_t *); diff --git a/plugins/NewStory/src/webpage.cpp b/plugins/NewStory/src/webpage.cpp index f4f8d7cd69..7ce346a17c 100644 --- a/plugins/NewStory/src/webpage.cpp +++ b/plugins/NewStory/src/webpage.cpp @@ -121,9 +121,6 @@ NSWebPage::NSWebPage(NewstoryListData &_1) : m_hClipRgn = NULL; m_tmp_hdc = GetDC(NULL); - GdiplusStartupInput gdiplusStartupInput; - GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); - EnumFonts(m_tmp_hdc, NULL, EnumFontsProc, (LPARAM)this); m_installed_fonts.insert(L"monospace"); m_installed_fonts.insert(L"serif"); @@ -135,7 +132,6 @@ NSWebPage::NSWebPage(NewstoryListData &_1) : NSWebPage::~NSWebPage() { clear_images(); - GdiplusShutdown(m_gdiplusToken); { mir_cslock lck(g_csMissingFiles); for (auto &it : g_arMissingFiles.rev_iter()) @@ -329,11 +325,6 @@ void NSWebPage::draw_list_marker(uint_ptr hdc, const list_marker &marker) release_clip((HDC)hdc); } -void NSWebPage::make_url_utf8(const char *url, const char *basepath, std::wstring &out) -{ - make_url(Utf2T(url), Utf2T(basepath), out); -} - Bitmap* NSWebPage::load_image(const wchar_t *pwszUrl, ItemData *pItem) { mir_cslockfull lck(m_csImages); @@ -351,13 +342,12 @@ Bitmap* NSWebPage::load_image(const wchar_t *pwszUrl, ItemData *pItem) if (!g_arMissingFiles.find(&tmp)) g_arMissingFiles.insert(new NSWebCache(tmp)); - return 0; + return g_plugin.m_pNoImage; } -void NSWebPage::load_image(const char *src, const char *baseurl, bool redraw_on_ready) +void NSWebPage::load_image(const char *src, const char */*baseurl*/, bool redraw_on_ready) { - std::wstring url; - make_url_utf8(src, baseurl, url); + std::wstring url = Utf2T(src); mir_cslockfull lck(m_csImages); if (m_images.count(url) == 0) { @@ -384,27 +374,25 @@ Bitmap* NSWebPage::find_image(const wchar_t *pwszUrl) return nullptr; } -void NSWebPage::get_image_size(const char *src, const char *baseurl, size &sz) +void NSWebPage::get_image_size(const char *src, const char * /*baseurl*/, size &sz) { - std::wstring url; - make_url_utf8(src, baseurl, url); + std::wstring url = Utf2T(src); mir_cslock lck(m_csImages); images_map::iterator img = m_images.find(url); if (img != m_images.end() && img->second) get_img_size(img->second, sz); else - sz.width = sz.height = 0; + sz.width = sz.height = 48; } void NSWebPage::clear_images() { mir_cslock lck(m_csImages); - for (auto &img : m_images) { - if (img.second) { - free_image(img.second); - } - } + for (auto &img : m_images) + if (img.second) + delete (Bitmap *)img.second; + m_images.clear(); } @@ -543,23 +531,16 @@ void NSWebPage::get_img_size(uint_ptr img, size &sz) } } -void NSWebPage::free_image(uint_ptr img) -{ - Bitmap *bmp = (Bitmap *)img; - delete bmp; -} - -void NSWebPage::draw_image(uint_ptr _hdc, const background_layer &bg, const std::string &src, const std::string &base_url) +void NSWebPage::draw_image(uint_ptr _hdc, const background_layer &bg, const std::string &src, const std::string& /*base_url*/) { if (src.empty() || (!bg.clip_box.width && !bg.clip_box.height)) return; - std::wstring url; - make_url_utf8(src.c_str(), base_url.c_str(), url); + std::wstring url = Utf2T(src.c_str()); Bitmap *bgbmp = find_image(url.c_str()); if (!bgbmp) - return; + bgbmp = g_plugin.m_pNoImage; Graphics graphics((HDC)_hdc); graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor); @@ -739,11 +720,6 @@ void NSWebPage::import_css(std::string &, const std::string &, std::string &) { } -void NSWebPage::make_url(LPCWSTR url, LPCWSTR, std::wstring &out) -{ - out = url; -} - void NSWebPage::on_anchor_click(const char *pszUtl, const element::ptr &) { Utf2T wszUrl(pszUtl); -- cgit v1.2.3