comparison foosdk/sdk/foobar2000/shared/font_description.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "shared.h"
2
3
4 static unsigned query_dpi()
5 {
6 unsigned ret;
7 HDC dc = GetDC(0);
8 ret = GetDeviceCaps(dc,LOGPIXELSY);
9 ReleaseDC(0,dc);
10 return ret;
11 }
12
13
14 #if 0
15 struct t_font_description
16 {
17 enum {m_facename_length = LF_FACESIZE*2};
18
19 t_uint32 m_height;
20 t_uint32 m_weight;
21 t_uint8 m_italic;
22 t_uint8 m_charset;
23 char m_facename[m_facename_length];
24 }
25 #endif
26
27 static void make_logfont(LOGFONT & p_logfont,const t_font_description & p_desc)
28 {
29 p_logfont.lfHeight = - MulDiv(p_desc.m_height, query_dpi(), t_font_description::m_height_dpi);
30 p_logfont.lfWidth = 0;
31 p_logfont.lfEscapement = 0;
32 p_logfont.lfOrientation = 0;
33 p_logfont.lfWeight = p_desc.m_weight;
34 p_logfont.lfItalic = p_desc.m_italic;
35 p_logfont.lfUnderline = 0;
36 p_logfont.lfStrikeOut = 0;
37 p_logfont.lfCharSet = p_desc.m_charset;
38 p_logfont.lfOutPrecision = 3;
39 p_logfont.lfClipPrecision = 2;
40 p_logfont.lfQuality = 1;
41 p_logfont.lfPitchAndFamily = 34;
42 pfc::stringToBuffer(p_logfont.lfFaceName,pfc::stringcvt::string_os_from_utf8(p_desc.m_facename,tabsize(p_desc.m_facename)));
43 }
44
45 static void make_description(t_font_description & p_desc,const LOGFONT & p_logfont)
46 {
47 p_desc.m_height = MulDiv(pfc::abs_t(p_logfont.lfHeight), t_font_description::m_height_dpi, query_dpi());
48 p_desc.m_weight = p_logfont.lfWeight;
49 p_desc.m_italic = p_logfont.lfItalic;
50 p_desc.m_charset = p_logfont.lfCharSet;
51 pfc::stringToBuffer(p_desc.m_facename,pfc::stringcvt::string_utf8_from_os(p_logfont.lfFaceName,tabsize(p_logfont.lfFaceName)));
52 }
53
54 HFONT SHARED_EXPORT t_font_description::create() const
55 {
56 LOGFONT temp;
57 make_logfont(temp,*this);
58 return CreateFontIndirect(&temp);
59 }
60
61 static UINT_PTR CALLBACK choose_font_hook(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
62 {
63 switch(msg)
64 {
65 case WM_INITDIALOG:
66 {
67 CHOOSEFONT * cf = reinterpret_cast<CHOOSEFONT*>(lp);
68 reinterpret_cast<modal_dialog_scope*>(cf->lCustData)->initialize(FindOwningPopup(wnd));
69 }
70 return 0;
71 default:
72 return 0;
73 }
74 }
75
76 bool SHARED_EXPORT t_font_description::popup_dialog(HWND p_parent)
77 {
78 modal_dialog_scope scope;
79
80 LOGFONT logfont;
81 make_logfont(logfont,*this);
82
83 CHOOSEFONT cf = {};
84 cf.lStructSize = sizeof(cf);
85 cf.hwndOwner = p_parent;
86 cf.lpLogFont = &logfont;
87 cf.Flags = CF_SCREENFONTS|CF_FORCEFONTEXIST|CF_INITTOLOGFONTSTRUCT|CF_ENABLEHOOK;
88 cf.nFontType = SCREEN_FONTTYPE;
89 cf.lCustData = reinterpret_cast<LPARAM>(&scope);
90 cf.lpfnHook = choose_font_hook;
91 if (ChooseFont(&cf))
92 {
93 make_description(*this,logfont);
94 return true;
95 }
96 else return false;
97 }
98
99
100 void SHARED_EXPORT t_font_description::from_font(HFONT p_font)
101 {
102 LOGFONT logfont;
103 PFC_ASSERT_SUCCESS( GetObject((HGDIOBJ) p_font, sizeof(logfont), &logfont) != 0 );
104 make_description(*this,logfont);
105 }
106
107 t_font_description SHARED_EXPORT t_font_description::g_from_font(HFONT p_font)
108 {
109 t_font_description temp;
110 temp.from_font(p_font);
111 return temp;
112 }
113
114
115 t_font_description SHARED_EXPORT t_font_description::g_from_logfont(LOGFONT const & lf) {
116 t_font_description ret; make_description(ret, lf); return ret;
117 }
118
119 t_font_description SHARED_EXPORT t_font_description::g_from_system(int id) {
120 LOGFONT lf;
121 if (FAILED( GetThemeSysFont(NULL, id, &lf) ) ) {
122 PFC_ASSERT(!"Should not get here!");
123 return g_from_font( (HFONT) GetStockObject(DEFAULT_GUI_FONT) );
124 }
125 return g_from_logfont(lf);
126 }