µGFX  2.9
version 2.9
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.io/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 gBool 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 gFalse;
38  pattern++;
39  name++;
40  break;
41  }
42  }
43 }
44 
45 gFont 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(gFont 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 gFont gdispScaleFont(gFont font, gU8 scale_x, gU8 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 (gFont)newfont;
87 }
88 
89 const char *gdispGetFontName(gFont font) {
90  return font->short_name;
91 }
92 
93 gBool gdispAddFont(gFont 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 gFalse;
98 
99  if (!(hdr = gfxAlloc(sizeof(struct mf_font_list_s))))
100  return gFalse;
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 gTrue;
109 }
110 
111 #endif /* GFX_USE_GDISP && GDISP_NEED_TEXT */
gFont gdispOpenFont(const char *name)
Find a font and return it.
void gdispCloseFont(gFont font)
Release a font after use.
gBool gdispAddFont(gFont font)
Add a font permanently to the font list.
const struct mf_font_s * gFont
The type of a font.
Definition: gdisp.h:93
const char * gdispGetFontName(gFont font)
Get the name of the specified font.
gFont gdispScaleFont(gFont font, gU8 scale_x, gU8 scale_y)
Make a scaled copy of an existing font.
void * gfxAlloc(gMemSize sz)
Allocate memory.
void gfxFree(void *ptr)
Free memory.