commit df95d3499594199e49fc8286ec2fbf1b8cb4ade1
parent bc6e539e6f24b1a69ab8142cffb99df5c38547de
Author: Michael Savage <mikejsavage@gmail.com>
Date: Thu, 6 Sep 2018 16:17:43 +0300
Runtime font selection on Windows
Diffstat:
4 files changed, 73 insertions(+), 24 deletions(-)
diff --git a/src/lua/main.lua b/src/lua/main.lua
@@ -15,7 +15,7 @@ local printMain, newlineMain, drawMain,
printChat, newlineChat, drawChat,
setHandlers, urgent, setStatus,
sock_connect, sock_send, sock_close,
- get_time = ...
+ get_time, set_font = ...
local socket_api = {
connect = sock_connect,
@@ -39,6 +39,15 @@ mud.drawChat = drawChat
mud.urgent = urgent
mud.now = get_time
+mud.alias( "/font", {
+ [ "^(.-)%s+(%d+)$" ] = function( name, size )
+ size = tonumber( size )
+ if not set_font( name, size ) then
+ mud.print( "\n#s> Couldn't set font" )
+ end
+ end,
+}, "<font name> <font size>" )
+
require( "status" ).init( setStatus )
setHandlers( handlers.input, handlers.macro, handlers.close, socket_data_handler, handlers.interval )
diff --git a/src/script.cc b/src/script.cc
@@ -233,6 +233,17 @@ extern "C" int mud_now( lua_State * L ) {
return 1;
}
+extern "C" int mud_set_font( lua_State * L ) {
+ const char * name = luaL_checkstring( L, 1 );
+ int size = luaL_checkinteger( L, 2 );
+
+ bool ok = ui_set_font( name, size );
+
+ lua_pushboolean( lua, ok );
+
+ return 1;
+}
+
} // anon namespace
#if PLATFORM_WINDOWS
@@ -281,7 +292,9 @@ void script_init() {
lua_pushcfunction( lua, mud_now );
- pcall( 13, "Error running main.lua" );
+ lua_pushcfunction( lua, mud_set_font );
+
+ pcall( 14, "Error running main.lua" );
}
void script_term() {
diff --git a/src/ui.h b/src/ui.h
@@ -47,4 +47,6 @@ void * platform_connect( const char ** err, const char * host, int port );
void platform_send( void * sock, const char * data, size_t len );
void platform_close( void * sock );
+bool ui_set_font( const char * name, int size );
+
void event_loop();
diff --git a/src/win32.cc b/src/win32.cc
@@ -102,7 +102,8 @@ static Socket * socket_from_fd( int fd ) {
struct MudFont {
int ascent;
int width, height;
- HFONT font;
+ HFONT regular;
+ HFONT bold;
};
struct {
@@ -111,7 +112,7 @@ struct {
COLORREF cursor;
MudFont font;
- MudFont bold_font;
+ HFONT dc_font;
union {
struct {
@@ -322,7 +323,7 @@ void ui_draw_char( int left, int top, char c, Colour colour, bool bold, bool for
return;
}
- SelectObject( UI.back_buffer, ( bold || force_bold_font ? Style.bold_font : Style.font ).font );
+ SelectObject( UI.back_buffer, ( bold || force_bold_font ? Style.font.bold : Style.font.regular ) );
SetTextColor( UI.back_buffer, get_colour( colour, bold ) );
TextOutA( UI.back_buffer, left, top + SPACING, &c, 1 );
@@ -435,6 +436,45 @@ void ui_urgent() {
FlashWindow( UI.hwnd, FALSE );
}
+bool ui_set_font( const char * name, int size ) {
+ HFONT regular = CreateFontA( size, 0, 0, 0, FW_REGULAR,
+ FALSE, FALSE, FALSE, DEFAULT_CHARSET,
+ OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FIXED_PITCH,
+ name );
+
+ if( regular == NULL )
+ return false;
+
+ HFONT bold = CreateFontA( size, 0, 0, 0, FW_BOLD,
+ FALSE, FALSE, FALSE, DEFAULT_CHARSET,
+ OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FIXED_PITCH,
+ name );
+
+ if( bold == NULL ) {
+ DeleteObject( regular );
+ return false;
+ }
+
+ if( Style.font.regular != NULL ) {
+ SelectObject( UI.hdc, Style.dc_font );
+ DeleteObject( Style.font.regular );
+ DeleteObject( Style.font.bold );
+ }
+
+ Style.font.regular = regular;
+ Style.font.bold = bold;
+ Style.dc_font = ( HFONT ) SelectObject( UI.hdc, Style.font.regular );
+
+ TEXTMETRIC metrics;
+ GetTextMetrics( UI.hdc, &metrics );
+
+ Style.font.height = metrics.tmHeight;
+ Style.font.width = metrics.tmAveCharWidth;
+ Style.font.ascent = metrics.tmAscent;
+
+ ui_draw();
+}
+
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
switch( msg ) {
case WM_CREATE: {
@@ -443,24 +483,6 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
SetBkMode( UI.back_buffer, TRANSPARENT );
- Style.font.font = CreateFont( 14, 0, 0, 0, FW_REGULAR,
- FALSE, FALSE, FALSE, DEFAULT_CHARSET,
- OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH,
- "Dina" );
- Style.bold_font.font = CreateFont( 14, 0, 0, 0, FW_BOLD,
- FALSE, FALSE, FALSE, DEFAULT_CHARSET,
- OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH,
- "Dina" );
-
- SelectObject( UI.hdc, Style.font.font );
-
- TEXTMETRIC metrics;
- GetTextMetrics( UI.hdc, &metrics );
-
- Style.font.height = metrics.tmHeight;
- Style.font.width = metrics.tmMaxCharWidth;
- Style.font.ascent = metrics.tmAscent;
-
Style.bg = RGB( 0x1a, 0x1a, 0x1a );
Style.status_bg = RGB( 0x33, 0x33, 0x33 );
Style.cursor = RGB( 0x00, 0xff, 0x00 );
@@ -483,6 +505,8 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
Style.Colours.lmagenta = RGB( 0x87, 0x5f, 0xff );
Style.Colours.lcyan = RGB( 0x29, 0xfb, 0xff );
Style.Colours.lwhite = RGB( 0xce, 0xdb, 0xde );
+
+ ui_set_font( "", 14 );
} break;
case WM_SIZE: {
@@ -563,7 +587,7 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
case WM_CHAR: {
if( wParam >= ' ' && wParam < 127 ) {
- char c = wParam;
+ char c = char( wParam );
input_add( &c, 1 );
draw_input();
}
@@ -754,6 +778,7 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin
}
UI = { };
+ Style = { };
textbox_init( &UI.main_text, SCROLLBACK_SIZE );
textbox_init( &UI.chat_text, CHAT_ROWS );