aboutsummaryrefslogtreecommitdiffstats
path: root/src/gdisp/gdisp_fonts.c
blob: 14164f0256e395d3c942452e961587f483df5af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#include "../../gfx.h"

#if GFX_USE_GDISP && GDISP_NEED_TEXT

#include "mcufont/mcufont.h"

#define FONT_FLAG_DYNAMIC	0x80		// Custom flag to indicate dynamically allocated font
#define FONT_FLAG_UNLISTED	0x40		// Custom flag to indicate font is not currently listed

static const struct mf_font_list_s *fontList;

/**
 * Match a pattern against the font name.
 */
static gBool matchfont(const char *pattern, const char *name) {
	while(1) {
		switch (pattern[0]) {
		case '*':
			if (name[0] == 0)
				return pattern[1] == 0;
			if (pattern[1] == name[0])
				pattern++;
			else
				name++;
			break;
		case 0:
			return name[0] == 0;
		default:
			if (name[0] != pattern[0])
				return gFalse;
			pattern++;
			name++;
			break;
		}
	}
}

gFont gdispOpenFont(const char *name) {
	const struct mf_font_list_s *fp;
	
	if (!fontList)
		fontList = mf_get_font_list();
		
	// Try the long names first
	for(fp = fontList; fp; fp = fp->next) {
		if (matchfont(name, fp->font->full_name))
			return fp->font;
	}

	// Try the short names if no long names match
	for(fp = fontList; fp; fp = fp->next) {
		if (matchfont(name, fp->font->short_name))
			return fp->font;
	}
	
	/* Return default builtin font.. better than nothing. */
	return mf_get_font_list()->font;
}

void gdispCloseFont(gFont font) {
	if ((font->flags & (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) == (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) {
		/* Make sure that no-one can successfully use font after closing */
		((struct mf_font_s *)font)->render_character = 0;
		
		/* Release the allocated memory */
		gfxFree((void *)font);
	}
}

gFont gdispScaleFont(gFont font, uint8_t scale_x, uint8_t scale_y)
{
	struct mf_scaledfont_s *newfont;
	
	if (!(newfont = gfxAlloc(sizeof(struct mf_scaledfont_s))))
		return 0;
	
	mf_scale_font(newfont, font, scale_x, scale_y);
	((struct mf_font_s *)newfont)->flags |= FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED;
	return (gFont)newfont;
}

const char *gdispGetFontName(gFont font) {
	return font->short_name;
}

gBool gdispAddFont(gFont font) {
	struct mf_font_list_s *hdr;

	if ((font->flags & (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) != (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED))
		return gFalse;
		
	if (!(hdr = gfxAlloc(sizeof(struct mf_font_list_s))))
		return gFalse;

	if (!fontList)
		fontList = mf_get_font_list();
	hdr->font = (const struct mf_font_s *)font;
	hdr->next = fontList;
	((struct mf_font_s *)font)->flags &= ~FONT_FLAG_UNLISTED;
	fontList = hdr;
	return gTrue;
}

#endif /* GFX_USE_GDISP && GDISP_NEED_TEXT */