add "best match" algorithm to wsfont and use this instead of a private

function in rasops.
This commit is contained in:
mlelstv 2015-04-18 11:23:58 +00:00
parent 2b32f2bf6f
commit c950730079
3 changed files with 44 additions and 65 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: rasops.c,v 1.72 2014/08/18 03:59:27 riastradh Exp $ */
/* $NetBSD: rasops.c,v 1.73 2015/04/18 11:23:58 mlelstv Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.72 2014/08/18 03:59:27 riastradh Exp $");
__KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.73 2015/04/18 11:23:58 mlelstv Exp $");
#include "opt_rasops.h"
#include "rasops_glue.h"
@ -180,57 +180,6 @@ void rasops_make_box_chars_alpha(struct rasops_info *);
extern int cold;
/*
* Rate a font based on how many character cells we would get out of it in the
* current video mode. Lower is better.
*/
static void
rasops_matches(struct wsdisplay_font *font, void *cookie, int ident)
{
struct rasops_matchdata *md = cookie;
struct rasops_info *ri = md->ri;
int cols, score = 1;
/* first weed out fonts the caller doesn't claim support for */
if (FONT_IS_ALPHA(font)) {
/*
* If we end up with a bitmap font and an alpha font with
* otherwise identical scores, prefer alpha
*/
score = 0;
if ((ri->ri_flg & RI_ENABLE_ALPHA) == 0)
return;
}
cols = ri->ri_width / font->fontwidth;
/*
* if the font is too small to allow 80 columns give those closer to
* 80 a better score but with a huge penalty compared to fonts that
* give us 80 columns or more
*/
if (cols < md->wantcols) {
score += 100000 + 2 * (md->wantcols - cols);
} else
score += 2 * cols;
DPRINTF("%s: %d\n", font->name, score);
if (score < md->bestscore) {
md->bestscore = score;
md->pick = font;
md->ident = ident;
}
}
static int
rasops_find(struct rasops_info *ri, int wantrows, int wantcols)
{
struct rasops_matchdata md =
{ ri, wantcols, wantrows, 1000000, NULL, 0};
wsfont_walk(rasops_matches, &md);
if (md.pick == NULL)
return -1;
return (wsfont_make_cookie(md.ident, WSDISPLAY_FONTORDER_L2R,
WSDISPLAY_FONTORDER_L2R));
}
/*
* Initialize a 'rasops_info' descriptor.
*/
@ -243,6 +192,7 @@ rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
/* Select a font if the caller doesn't care */
if (ri->ri_font == NULL) {
int cookie = -1;
int flags;
wsfont_init();
@ -254,7 +204,18 @@ rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
wantrows = RASOPS_DEFAULT_HEIGHT;
if (wantcols == 0)
wantcols = RASOPS_DEFAULT_WIDTH;
cookie = rasops_find(ri, wantrows, wantcols);
flags = WSFONT_FIND_BESTWIDTH | WSFONT_FIND_BITMAP;
if ((ri->ri_flg & RI_ENABLE_ALPHA) != 0)
flags |= WSFONT_FIND_ALPHA;
cookie = wsfont_find(NULL,
ri->ri_width / wantcols,
0,
0,
WSDISPLAY_FONTORDER_L2R,
WSDISPLAY_FONTORDER_L2R,
flags);
/*
* this means there is no supported font in the list

View File

@ -1,4 +1,4 @@
/* $NetBSD: wsfont.c,v 1.57 2015/01/25 20:09:42 christos Exp $ */
/* $NetBSD: wsfont.c,v 1.58 2015/04/18 11:23:58 mlelstv Exp $ */
/*-
* Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: wsfont.c,v 1.57 2015/01/25 20:09:42 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: wsfont.c,v 1.58 2015/04/18 11:23:58 mlelstv Exp $");
#include "opt_wsfont.h"
@ -570,6 +570,7 @@ int
wsfont_matches(struct wsdisplay_font *font, const char *name,
int width, int height, int stride, int flags)
{
int score = 10000;
/* first weed out fonts the caller doesn't claim support for */
if (FONT_IS_ALPHA(font)) {
@ -583,8 +584,16 @@ wsfont_matches(struct wsdisplay_font *font, const char *name,
if (height != 0 && font->fontheight != height)
return (0);
if (width != 0 && font->fontwidth != width)
return (0);
if (width != 0) {
if ((flags & WSFONT_FIND_BESTWIDTH) == 0) {
if (font->fontwidth != width)
return (0);
} else {
if (font->fontwidth > width)
return (0);
score -= min(width - font->fontwidth, 9999);
}
}
if (stride != 0 && font->stride != stride)
return (0);
@ -592,19 +601,27 @@ wsfont_matches(struct wsdisplay_font *font, const char *name,
if (name != NULL && strcmp(font->name, name) != 0)
return (0);
return (1);
return (score);
}
int
wsfont_find(const char *name, int width, int height, int stride, int bito, int byteo, int flags)
{
struct font *ent;
struct font *ent, *bestent = NULL;
int score, bestscore = 0;
TAILQ_FOREACH(ent, &list, chain) {
if (wsfont_matches(ent->font, name, width, height, stride, flags))
return (wsfont_make_cookie(ent->cookie, bito, byteo));
score = wsfont_matches(ent->font, name,
width, height, stride, flags);
if (score > bestscore) {
bestscore = score;
bestent = ent;
}
}
if (bestent != NULL)
return (wsfont_make_cookie(bestent->cookie, bito, byteo));
return (-1);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: wsfont.h,v 1.24 2012/01/11 15:52:32 macallan Exp $ */
/* $NetBSD: wsfont.h,v 1.25 2015/04/18 11:23:58 mlelstv Exp $ */
/*-
* Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
@ -62,9 +62,10 @@ struct wsdisplay_font;
void wsfont_init(void);
int wsfont_matches(struct wsdisplay_font *, const char *, int, int, int, int);
int wsfont_find(const char *, int, int, int, int, int, int);
#define WSFONT_FIND_BITMAP 1
#define WSFONT_FIND_ALPHA 2
#define WSFONT_FIND_BITMAP 0x01
#define WSFONT_FIND_ALPHA 0x02
#define WSFONT_FIND_ALL 0xff
#define WSFONT_FIND_BESTWIDTH 0x1000
void wsfont_walk(void (*)(struct wsdisplay_font *, void *, int), void *);
int wsfont_add(struct wsdisplay_font *, int);