version 2.8
gdisp_fonts.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 #include "../../gfx.h"
9 
10 #if GFX_USE_GDISP && GDISP_NEED_TEXT
11 
12 #include "mcufont/mcufont.h"
13 
14 #define FONT_FLAG_DYNAMIC 0x80 // Custom flag to indicate dynamically allocated font
15 #define FONT_FLAG_UNLISTED 0x40 // Custom flag to indicate font is not currently listed
16 
17 static const struct mf_font_list_s *fontList;
18 
19 /**
20  * Match a pattern against the font name.
21  */
22 static bool_t matchfont(const char *pattern, const char *name) {
23  while(1) {
24  switch (pattern[0]) {
25  case '*':
26  if (name[0] == 0)
27  return pattern[1] == 0;
28  if (pattern[1] == name[0])
29  pattern++;
30  else
31  name++;
32  break;
33  case 0:
34  return name[0] == 0;
35  default:
36  if (name[0] != pattern[0])
37  return FALSE;
38  pattern++;
39  name++;
40  break;
41  }
42  }
43 }
44 
45 font_t gdispOpenFont(const char *name) {
46  const struct mf_font_list_s *fp;
47 
48  if (!fontList)
49  fontList = mf_get_font_list();
50 
51  // Try the long names first
52  for(fp = fontList; fp; fp = fp->next) {
53  if (matchfont(name, fp->font->full_name))
54  return fp->font;
55  }
56 
57  // Try the short names if no long names match
58  for(fp = fontList; fp; fp = fp->next) {
59  if (matchfont(name, fp->font->short_name))
60  return fp->font;
61  }
62 
63  /* Return default builtin font.. better than nothing. */
64  return mf_get_font_list()->font;
65 }
66 
67 void gdispCloseFont(font_t font) {
68  if ((font->flags & (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) == (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) {
69  /* Make sure that no-one can successfully use font after closing */
70  ((struct mf_font_s *)font)->render_character = 0;
71 
72  /* Release the allocated memory */
73  gfxFree((void *)font);
74  }
75 }
76 
77 font_t gdispScaleFont(font_t font, uint8_t scale_x, uint8_t scale_y)
78 {
79  struct mf_scaledfont_s *newfont;
80 
81  if (!(newfont = gfxAlloc(sizeof(struct mf_scaledfont_s))))
82  return 0;
83 
84  mf_scale_font(newfont, font, scale_x, scale_y);
85  ((struct mf_font_s *)newfont)->flags |= FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED;
86  return (font_t)newfont;
87 }
88 
89 const char *gdispGetFontName(font_t font) {
90  return font->short_name;
91 }
92 
93 bool_t gdispAddFont(font_t font) {
94  struct mf_font_list_s *hdr;
95 
96  if ((font->flags & (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED)) != (FONT_FLAG_DYNAMIC|FONT_FLAG_UNLISTED))
97  return FALSE;
98 
99  if (!(hdr = gfxAlloc(sizeof(struct mf_font_list_s))))
100  return FALSE;
101 
102  if (!fontList)
103  fontList = mf_get_font_list();
104  hdr->font = (const struct mf_font_s *)font;
105  hdr->next = fontList;
106  ((struct mf_font_s *)font)->flags &= ~FONT_FLAG_UNLISTED;
107  fontList = hdr;
108  return TRUE;
109 }
110 
111 #endif /* GFX_USE_GDISP && GDISP_NEED_TEXT */
const struct mf_font_s * font_t
The type of a font.
Definition: gdisp.h:93
bool_t gdispAddFont(font_t font)
Add a font permanently to the font list.
#define FALSE
Generic 'false' boolean constant.
Definition: gfx.h:31
void * gfxAlloc(size_t sz)
Allocate memory.
const char * gdispGetFontName(font_t font)
Get the name of the specified font.
font_t gdispOpenFont(const char *name)
Find a font and return it.
void gfxFree(void *ptr)
Free memory.
void gdispCloseFont(font_t font)
Release a font after use.
font_t gdispScaleFont(font_t font, uint8_t scale_x, uint8_t scale_y)
Make a scaled copy of an existing font.
#define TRUE
Generic 'true' boolean constant.
Definition: gfx.h:38