choose font sizes to fit

Make on demand rather than guessing. Only wind up with six or so, so the
linear search is probably ok.
This commit is contained in:
Eric House 2021-02-11 18:31:31 -08:00
parent fddac79dd8
commit 7dc949fb38
3 changed files with 46 additions and 29 deletions

View file

@ -1,6 +1,6 @@
/* -*- compile-command: "cd ../linux && make MEMDEBUG=TRUE -j3"; -*- */ /* -*- compile-command: "cd ../linux && make MEMDEBUG=TRUE -j3"; -*- */
/* /*
* Copyright 1997 - 2011 by Eric House (xwords@eehouse.org). All rights * Copyright 1997 - 2021 by Eric House (xwords@eehouse.org). All rights
* reserved. * reserved.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or

View file

@ -136,6 +136,7 @@ initDeviceGlobals( Globals* globals )
globals->cp.allowPeek = XP_TRUE; globals->cp.allowPeek = XP_TRUE;
// globals->cp.showRobotScores = XP_TRUE; // globals->cp.showRobotScores = XP_TRUE;
globals->cp.sortNewTiles = XP_TRUE; globals->cp.sortNewTiles = XP_TRUE;
globals->cp.showColors = XP_TRUE;
globals->procs.send = send_msg; globals->procs.send = send_msg;
globals->procs.closure = globals; globals->procs.closure = globals;

View file

@ -26,15 +26,19 @@
#include "wasmdraw.h" #include "wasmdraw.h"
#include "dbgutil.h" #include "dbgutil.h"
typedef struct FontRec{
struct FontRec* next;
int size;
TTF_Font* font;
} FontRec;
typedef struct _WasmDrawCtx { typedef struct _WasmDrawCtx {
DrawCtxVTable* vtable; DrawCtxVTable* vtable;
SDL_Renderer* renderer; SDL_Renderer* renderer;
SDL_Surface* surface; SDL_Surface* surface;
SDL_Texture* texture; SDL_Texture* texture;
TTF_Font* font12;
TTF_Font* font20; FontRec* fonts;
TTF_Font* font36;
TTF_Font* font48;
SDL_Surface* arrowDown; SDL_Surface* arrowDown;
SDL_Surface* arrowRight; SDL_Surface* arrowRight;
@ -79,6 +83,31 @@ enum { BLACK,
N_COLORS, N_COLORS,
}; };
static TTF_Font*
fontFor( WasmDrawCtx* wdctx, int height )
{
TTF_Font* result = NULL;
for ( FontRec* rec = wdctx->fonts; !!rec; rec = rec->next ) {
if ( rec->size == height ) {
result = rec->font;
break;
}
}
if ( !result ) {
FontRec* rec = XP_MALLOC( wdctx->mpool, sizeof(*rec) );
rec->next = wdctx->fonts;
wdctx->fonts = rec;
rec->size = height;
rec->font = TTF_OpenFont( "assets_dir/FreeSans.ttf", height );
result = rec->font;
XP_LOGFF( "made font for size %d", height );
}
return result;
}
static SDL_Color sOtherColors[N_COLORS] = { static SDL_Color sOtherColors[N_COLORS] = {
{0x00, 0x00, 0x00, 0xFF}, /* BLACK */ {0x00, 0x00, 0x00, 0xFF}, /* BLACK */
{0xFF, 0xFF, 0xFF, 0xFF}, /* WHITE */ {0xFF, 0xFF, 0xFF, 0xFF}, /* WHITE */
@ -150,16 +179,7 @@ static void
textInRect( WasmDrawCtx* wdctx, const XP_UCHAR* text, const XP_Rect* rect, textInRect( WasmDrawCtx* wdctx, const XP_UCHAR* text, const XP_Rect* rect,
const SDL_Color* color ) const SDL_Color* color )
{ {
TTF_Font* font; TTF_Font* font = fontFor( wdctx, rect->height );
if ( rect->height <= 12 ) {
font = wdctx->font12;
} else if ( rect->height <= 20 ) {
font = wdctx->font20;
} else if ( rect->height <= 36 ) {
font = wdctx->font36;
} else {
font = wdctx->font48;
}
XP_Rect tmpR = *rect; XP_Rect tmpR = *rect;
@ -248,10 +268,12 @@ wasm_draw_destroyCtxt( DrawCtx* dctx, XWEnv xwe )
LOG_FUNC(); LOG_FUNC();
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
TTF_CloseFont( wdctx->font12 ); FontRec* next = NULL;
TTF_CloseFont( wdctx->font20 ); for ( FontRec* rec = wdctx->fonts; !!rec; rec = next ) {
TTF_CloseFont( wdctx->font36 ); TTF_CloseFont( rec->font );
TTF_CloseFont( wdctx->font48 ); next = rec->next;
XP_FREE( wdctx->mpool, rec );
}
XP_FREEP( wdctx->mpool, &wdctx->vtable ); XP_FREEP( wdctx->mpool, &wdctx->vtable );
XP_FREEP( wdctx->mpool, &wdctx ); XP_FREEP( wdctx->mpool, &wdctx );
@ -485,10 +507,10 @@ wasm_draw_drawCell( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
textInRect( wdctx, text, &tmpRect, foreColor ); textInRect( wdctx, text, &tmpRect, foreColor );
if ( !!valueStr ) { if ( !!valueStr ) {
XP_Rect tmpRect = *rect; XP_Rect tmpRect = *rect;
tmpRect.left += tmpRect.width * 2 / 3; tmpRect.left += tmpRect.width * 1 / 2;
tmpRect.top += tmpRect.height * 2 / 3; tmpRect.top += tmpRect.height * 1 / 2;
tmpRect.width /= 3; tmpRect.width /= 2;
tmpRect.height /= 3; tmpRect.height /= 2;
textInRect( wdctx, valueStr, &tmpRect, foreColor ); textInRect( wdctx, valueStr, &tmpRect, foreColor );
} }
} }
@ -616,12 +638,6 @@ wasm_draw_make( MPFORMAL int width, int height )
WasmDrawCtx* wdctx = XP_MALLOC( mpool, sizeof(*wdctx) ); WasmDrawCtx* wdctx = XP_MALLOC( mpool, sizeof(*wdctx) );
MPASSIGN( wdctx->mpool, mpool ); MPASSIGN( wdctx->mpool, mpool );
wdctx->font12 = TTF_OpenFont( "assets_dir/FreeSans.ttf", 12 );
XP_ASSERT( !!wdctx->font12 );
wdctx->font20 = TTF_OpenFont( "assets_dir/FreeSans.ttf", 20 );
wdctx->font36 = TTF_OpenFont( "assets_dir/FreeSans.ttf", 36 );
wdctx->font48 = TTF_OpenFont( "assets_dir/FreeSans.ttf", 48 );
wdctx->arrowDown = IMG_Load( "assets_dir/ic_downarrow.png" ); wdctx->arrowDown = IMG_Load( "assets_dir/ic_downarrow.png" );
wdctx->arrowRight = IMG_Load( "assets_dir/ic_rightarrow.png" ); wdctx->arrowRight = IMG_Load( "assets_dir/ic_rightarrow.png" );
wdctx->origin = IMG_Load( "assets_dir/ic_origin.png" ); wdctx->origin = IMG_Load( "assets_dir/ic_origin.png" );