Skip to content

Commit 3ba55f3

Browse files
committed
make fb_tooltip utilize the font cache, misc cleanup
1 parent d64ba45 commit 3ba55f3

File tree

7 files changed

+70
-67
lines changed

7 files changed

+70
-67
lines changed

foo_spider_monkey_panel/js_objects/fb_tooltip.cpp

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include "fb_tooltip.h"
44

55
#include <js_engine/js_to_native_invoker.h>
6+
67
#include <js_utils/js_error_helper.h>
8+
#include <js_utils/js_hwnd_helpers.h>
79
#include <js_utils/js_object_helper.h>
810

911
#include <qwr/final_action.h>
@@ -81,9 +83,9 @@ JsFbTooltip::JsFbTooltip( JSContext* cx, HWND hParentWnd )
8183
: pJsCtx_( cx )
8284
, hParentWnd_( hParentWnd )
8385
, tipBuffer_( TEXT( SMP_NAME ) )
84-
, pFont_( smp::gdi::CreateUniquePtr<HFONT>( nullptr ) )
8586
{
86-
hTooltipWnd_ = CreateWindowEx(
87+
hTooltipWnd_ = CreateWindowEx
88+
(
8789
WS_EX_TOPMOST,
8890
TOOLTIPS_CLASS,
8991
nullptr,
@@ -95,14 +97,14 @@ JsFbTooltip::JsFbTooltip( JSContext* cx, HWND hParentWnd )
9597
hParentWnd_,
9698
nullptr,
9799
core_api::get_my_instance(),
98-
nullptr );
100+
nullptr
101+
);
102+
99103
qwr::error::CheckWinApi( hTooltipWnd_, "CreateWindowEx" );
100104

101105
qwr::final_action autoHwnd( [hTooltipWnd = hTooltipWnd_] {
102106
if ( IsWindow( hTooltipWnd ) )
103-
{
104107
DestroyWindow( hTooltipWnd );
105-
}
106108
} );
107109

108110
// Original position
@@ -173,46 +175,39 @@ void JsFbTooltip::SetDelayTime( uint32_t type, int32_t time )
173175
SendMessage( hTooltipWnd_, TTM_SETDELAYTIME, type, static_cast<LPARAM>( static_cast<int>( MAKELONG( time, 0 ) ) ) );
174176
}
175177

176-
void JsFbTooltip::SetFont( const std::wstring& name, uint32_t pxSize, uint32_t style )
178+
void JsFbTooltip::SetFont( const std::wstring& fontName, int32_t fontSize, uint32_t fontStyle )
177179
{
178-
fontName_ = name;
179-
fontSize_ = pxSize;
180-
fontStyle_ = style;
180+
LOGFONTW logfont;
181+
logfont::Make( fontName, fontSize, fontStyle, logfont );
181182

182-
if ( !fontName_.empty() )
183-
{
184-
pFont_.reset( CreateFont(
185-
// from msdn: "< 0, The font mapper transforms this value into device units
186-
// and matches its absolute value against the character height of the available fonts."
187-
-static_cast<int>( fontSize_ ),
188-
0,
189-
0,
190-
0,
191-
( fontStyle_ & Gdiplus::FontStyleBold ) ? FW_BOLD : FW_NORMAL,
192-
( fontStyle_ & Gdiplus::FontStyleItalic ) ? TRUE : FALSE,
193-
( fontStyle_ & Gdiplus::FontStyleUnderline ) ? TRUE : FALSE,
194-
( fontStyle_ & Gdiplus::FontStyleStrikeout ) ? TRUE : FALSE,
195-
DEFAULT_CHARSET,
196-
OUT_DEFAULT_PRECIS,
197-
CLIP_DEFAULT_PRECIS,
198-
DEFAULT_QUALITY,
199-
DEFAULT_PITCH | FF_DONTCARE,
200-
fontName_.c_str() ) );
201-
qwr::error::CheckWinApi( !!pFont_, "CreateFont" );
202-
SendMessage( hTooltipWnd_, WM_SETFONT, (WPARAM)pFont_.get(), MAKELPARAM( FALSE, 0 ) );
203-
}
183+
#if FONT_CACHE_ABSOLUTE_HEIGHT
184+
const HWND wnd = GetPanelHwndForCurrentGlobal( pJsCtx_ );
185+
const HDC dc = GetDC( wnd );
186+
qwr::final_action autoHdcReleaser( [wnd, dc] { ReleaseDC( wnd, dc ); } );
187+
logfont::Normalize( dc, logfont );
188+
#endif
189+
190+
fontcache::shared_hfont hfont = fontcache::Cache( logfont );
191+
192+
if ( font == hfont )
193+
return;
194+
195+
font = hfont;
196+
197+
SendMessage( hTooltipWnd_, WM_SETFONT, (WPARAM)font.get(), MAKELPARAM( FALSE, 0 ) );
204198
}
205199

206-
void JsFbTooltip::SetFontWithOpt( size_t optArgCount, const std::wstring& name, uint32_t pxSize, uint32_t style )
200+
void JsFbTooltip::SetFontWithOpt( size_t optArgCount,
201+
const std::wstring& fontName, int32_t fontSize, uint32_t fontStyle )
207202
{
208203
switch ( optArgCount )
209204
{
210205
case 0:
211-
return SetFont( name, pxSize, style );
206+
return SetFont( fontName, fontSize, fontStyle );
212207
case 1:
213-
return SetFont( name, pxSize );
208+
return SetFont( fontName, fontSize );
214209
case 2:
215-
return SetFont( name );
210+
return SetFont( fontName );
216211
default:
217212
throw qwr::QwrException( "Internal error: invalid number of optional arguments specified: {}", optArgCount );
218213
}

foo_spider_monkey_panel/js_objects/fb_tooltip.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <js_objects/object_base.h>
4-
#include <utils/gdi_helpers.h>
4+
#include <utils/font_cache.h>
55

66
#include <memory>
77
#include <optional>
@@ -13,6 +13,7 @@ struct JSClass;
1313

1414
namespace mozjs
1515
{
16+
using namespace smp;
1617

1718
class JsFbTooltip
1819
: public JsObjectBase<JsFbTooltip>
@@ -42,8 +43,8 @@ class JsFbTooltip
4243
void Deactivate();
4344
uint32_t GetDelayTime( uint32_t type );
4445
void SetDelayTime( uint32_t type, int32_t time );
45-
void SetFont( const std::wstring& name, uint32_t pxSize = 12, uint32_t style = 0 );
46-
void SetFontWithOpt( size_t optArgCount, const std::wstring& name, uint32_t pxSize, uint32_t style );
46+
void SetFont( const std::wstring& fontName, int32_t fontSize = 0, uint32_t fontStyle = 0 );
47+
void SetFontWithOpt( size_t optArgCount, const std::wstring& fontName, int32_t fontSize, uint32_t fontStyle );
4748
void SetMaxWidth( uint32_t width );
4849
void TrackPosition( int x, int y );
4950

@@ -61,12 +62,9 @@ class JsFbTooltip
6162
HWND hTooltipWnd_ = nullptr;
6263
HWND hParentWnd_ = nullptr;
6364

64-
std::wstring fontName_;
65-
uint32_t fontSize_{};
66-
uint32_t fontStyle_{};
6765
std::wstring tipBuffer_;
6866

69-
smp::gdi::unique_gdi_ptr<HFONT> pFont_;
67+
fontcache::shared_hfont font = nullptr;
7068
std::unique_ptr<TOOLINFO> toolInfo_;
7169
};
7270

foo_spider_monkey_panel/js_objects/gdi_graphics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#include "gdi_graphics.h"
44

55
#include <js_engine/js_to_native_invoker.h>
6+
67
#include <js_objects/gdi_bitmap.h>
78
#include <js_objects/gdi_font.h>
89
#include <js_objects/gdi_raw_bitmap.h>
910
#include <js_objects/measure_string_info.h>
11+
1012
#include <js_utils/js_error_helper.h>
1113
#include <js_utils/js_object_helper.h>
1214
#include <js_utils/js_property_helper.h>
15+
1316
#include <utils/colour_helpers.h>
1417
#include <utils/gdi_error_helpers.h>
1518
#include <utils/text_helpers.h>

foo_spider_monkey_panel/js_objects/gdi_raw_bitmap.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
#include "gdi_raw_bitmap.h"
44

55
#include <js_engine/js_to_native_invoker.h>
6+
67
#include <js_utils/js_error_helper.h>
8+
#include <js_utils/js_hwnd_helpers.h>
79
#include <js_utils/js_object_helper.h>
10+
811
#include <utils/gdi_error_helpers.h>
912
#include <utils/gdi_helpers.h>
1013

14+
#include <qwr/final_action.h>
1115
#include <qwr/winapi_error_helpers.h>
1216

1317
using namespace smp;
@@ -81,14 +85,20 @@ JsGdiRawBitmap::CreateNative( JSContext* cx, Gdiplus::Bitmap* pBmp )
8185
{
8286
qwr::QwrException::ExpectTrue( pBmp, "Internal error: Gdiplus::Bitmap is null" );
8387

84-
auto pDc = gdi::CreateUniquePtr( CreateCompatibleDC( nullptr ) );
88+
const HWND wnd = GetPanelHwndForCurrentGlobal( cx );
89+
const HDC dc = GetDC( wnd );
90+
qwr::final_action autoHdcReleaser( [wnd, dc] { ReleaseDC( wnd, dc ); } );
91+
92+
gdi::unique_gdi_ptr<HDC> pDc( CreateCompatibleDC( dc ) );
8593
qwr::error::CheckWinApi( !!pDc, "CreateCompatibleDC" );
8694

87-
auto hBitmap = gdi::CreateHBitmapFromGdiPlusBitmap( *pBmp );
95+
gdi::unique_gdi_ptr<HBITMAP> hBitmap = gdi::CreateHBitmapFromGdiPlusBitmap( *pBmp );
8896
qwr::QwrException::ExpectTrue( !!hBitmap, "Internal error: failed to get HBITMAP from Gdiplus::Bitmap" );
8997

90-
return std::unique_ptr<JsGdiRawBitmap>(
91-
new JsGdiRawBitmap( cx, std::move( pDc ), std::move( hBitmap ), pBmp->GetWidth(), pBmp->GetHeight() ) );
98+
return std::unique_ptr<JsGdiRawBitmap>
99+
(
100+
new JsGdiRawBitmap( cx, std::move( pDc ), std::move( hBitmap ), pBmp->GetWidth(), pBmp->GetHeight() )
101+
);
92102
}
93103

94104
size_t JsGdiRawBitmap::GetInternalSize( Gdiplus::Bitmap* pBmp )

foo_spider_monkey_panel/js_objects/gdi_utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
#include "gdi_utils.h"
44

55
#include <js_engine/js_to_native_invoker.h>
6+
67
#include <js_objects/gdi_bitmap.h>
78
#include <js_objects/gdi_font.h>
9+
810
#include <js_utils/js_error_helper.h>
911
#include <js_utils/js_hwnd_helpers.h>
1012
#include <js_utils/js_image_helpers.h>
1113
#include <js_utils/js_object_helper.h>
14+
1215
#include <utils/gdi_error_helpers.h>
1316
#include <utils/gdi_helpers.h>
17+
1418
#include <utils/image_helpers.h>
1519

1620
#include <qwr/winapi_error_helpers.h>

foo_spider_monkey_panel/utils/gdi_helpers.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ namespace smp::gdi
77

88
unique_gdi_ptr<HBITMAP> CreateHBitmapFromGdiPlusBitmap( Gdiplus::Bitmap& bitmap )
99
{
10-
const Gdiplus::Rect rect{ 0, 0, static_cast<int>( bitmap.GetWidth() ), static_cast<int>( bitmap.GetHeight() ) };
10+
const Gdiplus::Rect rect{ 0, 0, static_cast<INT>( bitmap.GetWidth() ), static_cast<INT>( bitmap.GetHeight() ) };
1111
Gdiplus::BitmapData bmpdata{};
1212

1313
if ( bitmap.LockBits( &rect, Gdiplus::ImageLockModeRead, PixelFormat32bppPARGB, &bmpdata ) != Gdiplus::Ok )
14-
{ // Error
15-
return CreateUniquePtr( HBITMAP( nullptr ) );
16-
}
17-
18-
BITMAP bm{};
19-
bm.bmType = 0;
20-
bm.bmWidth = bmpdata.Width;
21-
bm.bmHeight = bmpdata.Height;
22-
bm.bmWidthBytes = bmpdata.Stride;
23-
bm.bmPlanes = 1;
24-
bm.bmBitsPixel = 32;
25-
bm.bmBits = bmpdata.Scan0;
14+
return unique_gdi_ptr<HBITMAP>( nullptr ); // Error -> return null handle
15+
16+
BITMAP bm
17+
{
18+
0, // bmType
19+
static_cast<LONG>( bmpdata.Width ), // bmWidth
20+
static_cast<LONG>( bmpdata.Height ), // bmheight
21+
bmpdata.Stride, // bmWidthBytes
22+
1, // bmPlanes
23+
32, // bmBitsPixel
24+
bmpdata.Scan0 // bmBits
25+
};
2626

2727
HBITMAP hBitmap = CreateBitmapIndirect( &bm );
2828
bitmap.UnlockBits( &bmpdata );
2929

30-
return CreateUniquePtr( hBitmap );
30+
return unique_gdi_ptr<HBITMAP>( hBitmap );
3131
}
3232

3333
} // namespace smp::gdi

foo_spider_monkey_panel/utils/gdi_helpers.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ template <typename Hx>
7676
requires std::is_any<Hx, HGDIOBJ, HDC, HPEN, HBRUSH, HRGN, HPALETTE, HFONT, HBITMAP>::value
7777
using unique_gdi_ptr = std::unique_ptr<TxOF<Hx>>;
7878

79-
template <typename Hx>
80-
requires std::is_any<Hx, HGDIOBJ, HDC, HPEN, HBRUSH, HRGN, HPALETTE, HFONT, HBITMAP>::value
81-
[[nodiscard]] unique_gdi_ptr<Hx> CreateUniquePtr( Hx obj )
82-
{
83-
return unique_gdi_ptr<Hx>( obj );
84-
}
85-
8679
template <typename Hx>
8780
requires std::is_any<Hx, HGDIOBJ, HPEN, HBRUSH, HRGN, HPALETTE, HFONT, HBITMAP>::value
8881
class ObjectSelector

0 commit comments

Comments
 (0)