Skip to content

Commit

Permalink
Added GUI for selecting font.
Browse files Browse the repository at this point in the history
+ Fixed a bug that didn't load/render Japanese font correctly.
  • Loading branch information
sigma-axis committed Apr 18, 2024
1 parent 72424fc commit d7bfe61
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 12 deletions.
8 changes: 4 additions & 4 deletions color_loupe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class Handle {
};
static HFONT create_font(const wchar_t* name, int size) {
return ::CreateFontW(
size, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
size, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, name);
}
Expand Down Expand Up @@ -1397,8 +1397,8 @@ static inline bool open_settings(HWND hwnd)
// re-apply the zoom level, as its valid range might have changed.
apply_zoom(loupe_state.zoom.zoom_level, 0, 0);

// no need to discard font handles for new font settings,
// as there's no option at this point yet.
// discard font handles for new font settings.
ext_obj.free();
return true;
}
return false;
Expand Down Expand Up @@ -1844,7 +1844,7 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID lpvReserved)
// 看板.
////////////////////////////////
#define PLUGIN_NAME "色ルーペ"
#define PLUGIN_VERSION "v2.10-beta3"
#define PLUGIN_VERSION "v2.10-beta4"
#define PLUGIN_AUTHOR "sigma-axis"
#define PLUGIN_INFO_FMT(name, ver, author) (name##" "##ver##" by "##author)
#define PLUGIN_INFO PLUGIN_INFO_FMT(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
Expand Down
Binary file modified color_loupe.rc
Binary file not shown.
112 changes: 105 additions & 7 deletions dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ static inline int get_slider_value(HWND slider) {
return static_cast<int>(::SendMessageW(slider, TBM_GETPOS, 0, 0));
}


////////////////////////////////
// Internal messages.
////////////////////////////////
struct PrvMsg {
enum : uint32_t {
UpdateView = WM_USER + 64,
Expand Down Expand Up @@ -780,7 +784,10 @@ class tip_drag_metrics : public dialog_base {
macro(6, chrome_pad_v);\
macro(7, chrome_margin_h);\
macro(8, chrome_margin_v)
#define decl_on_change(_, field) void on_change_##field(int8_t data_new) { drag.##field = data_new; }
#define decl_on_change(_, field) \
void on_change_##field(int8_t data_new) {\
drag.##field = std::clamp(data_new, drag.##field##_min, drag.##field##_max);\
}
populate_field(decl_on_change);
#undef decl_on_change

Expand Down Expand Up @@ -1176,7 +1183,10 @@ class toast_metrics : public dialog_base {
macro(4, chrome_pad_v);\
macro(5, chrome_margin_h);\
macro(6, chrome_margin_v)
#define decl_on_change(_, field) void on_change_##field(int8_t data_new) { toast.##field = data_new; }
#define decl_on_change(_, field) \
void on_change_##field(int8_t data_new) {\
toast.##field = std::clamp(data_new, toast.##field##_min, toast.##field##_max);\
}
populate_field(decl_on_change);
#undef decl_on_change

Expand All @@ -1198,8 +1208,8 @@ class toast_metrics : public dialog_base {

auto font = dialogs::ExtFunc::CreateUprightFont(toast.font_name, toast.font_size);
dialogs::ExtFunc::DrawToast(cvs.hdc(), cvs.sz(),
sample.size() <= 1 ? res_str::get(IDS_DLG_TOAST_SAMPLE) : sample.data(),
nullptr, toast, color_scheme);
sample.size() <= 1 ? res_str::get(IDS_DLG_TOAST_SAMPLE) : sample.c_str(),
font, toast, color_scheme);
::DeleteObject(font);

::BitBlt(hdc, rc.left, rc.top, cvs.wd(), cvs.ht(), cvs.hdc(), 0, 0, SRCCOPY);
Expand Down Expand Up @@ -1341,6 +1351,88 @@ class grid_options : public dialog_base {
};


////////////////////////////////
// フォントの設定.
////////////////////////////////
class font_settings : public dialog_base {
using Pivot = Settings::WheelZoom::Pivot;
using ClickActions = Settings::ClickActions;

public:
wchar_t (&name)[LF_FACESIZE];
int8_t& size;
int8_t const min, max;
dialog_base* const update_target;
font_settings(wchar_t(&name)[LF_FACESIZE], int8_t& size, int8_t min, int8_t max,
dialog_base* const update_target)
: name{ name }, size{ size }, min{ min }, max{ max }, update_target{ update_target } {}

private:
void on_change_name(HWND ctrl) {
::SendMessageW(ctrl, WM_GETTEXT, std::size(name), reinterpret_cast<LPARAM>(name));
}
void on_change_size(int8_t data_new) { size = std::clamp(data_new, min, max); }
void update_view() {
if (update_target != nullptr && update_target->hwnd != nullptr)
::SendMessageW(update_target->hwnd, PrvMsg::UpdateView, {}, {});
}

protected:
uintptr_t template_id() const override { return IDD_SETTINGS_FORM_FONT; }

bool on_init(HWND) override
{
// suppress notifications from controls.
auto sc = suppress_callback();

// adding fonts to the combo box.
auto combo = ::GetDlgItem(hwnd, IDC_COMBO1);
HDC hdc = ::GetDC(nullptr);
::EnumFontFamiliesW(hdc, nullptr, [](const LOGFONTW* lf, auto, auto, LPARAM data) {
if (lf->lfFaceName[0] != L'@')
::SendMessageW(reinterpret_cast<HWND>(data), CB_ADDSTRING,
{}, reinterpret_cast<LPARAM>(lf->lfFaceName));
return TRUE;
}, reinterpret_cast<LPARAM>(combo));
::ReleaseDC(nullptr, hdc);
::SendMessageW(combo, CB_SELECTSTRING, -1, reinterpret_cast<LPARAM>(name));

// font size.
init_spin(::GetDlgItem(hwnd, IDC_SPIN1), size, min, max);

return false;
}

bool handler(UINT message, WPARAM wparam, LPARAM lparam) override
{
auto ctrl = reinterpret_cast<HWND>(lparam);
switch (message) {
case WM_COMMAND:
switch (auto id = 0xffff & wparam, code = wparam >> 16; code) {
case CBN_SELCHANGE:
switch (id) {
case IDC_COMBO1:
on_change_name(ctrl);
update_view();
return true;
}
break;
case EN_CHANGE:
switch (id) {
case IDC_EDIT1:
on_change_size(get_spin_value(::GetDlgItem(hwnd, IDC_SPIN1)));
update_view();
return true;
}
break;
}
break;
}
return false;
}
};


////////////////////////////////
// 一部コマンドの設定.
////////////////////////////////
Expand Down Expand Up @@ -1568,10 +1660,13 @@ class setting_dlg : public dialog_base {
new drag_keys{ curr.tip_drag.keys },
new drag_range{ curr.tip_drag.range },
new wheel_zoom{ curr.tip_drag.wheel },
update_target,
new tip_drag{ curr.tip_drag },
update_target,
new font_settings{
curr.tip_drag.font_name, curr.tip_drag.font_size,
curr.tip_drag.font_size_min, curr.tip_drag.font_size_max, update_target
},
new tip_drag_format{ curr.tip_drag, update_target },
// TODO: fonts?
};
case tab_kind::drag_exedit:
return new vscroll_form{
Expand Down Expand Up @@ -1632,7 +1727,10 @@ class setting_dlg : public dialog_base {
return new vscroll_form{
new toast_functions{ curr.toast, update_target },
update_target,
// TODO: fonts?
new font_settings{
curr.toast.font_name, curr.toast.font_size,
curr.toast.font_size_min, curr.toast.font_size_max, update_target
},
};

// TODO: color?
Expand Down
3 changes: 2 additions & 1 deletion resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#define IDD_SETTINGS_FORM_CMD_ZOOM_STEP 816
#define IDD_SETTINGS_FORM_CMD_CLIPBOARD 817
#define IDD_SETTINGS_FORM_CMD_DESC 818
#define IDD_SETTINGS_FORM_FONT 819
#define IDC_LIST1 1001
#define IDC_COMBO1 1002
#define IDC_COMBO2 1003
Expand Down Expand Up @@ -160,7 +161,7 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 819
#define _APS_NEXT_RESOURCE_VALUE 820
#define _APS_NEXT_COMMAND_VALUE 40013
#define _APS_NEXT_CONTROL_VALUE 1039
#define _APS_NEXT_SYMED_VALUE 101
Expand Down

0 comments on commit d7bfe61

Please sign in to comment.