video: Add a way to change the font name and size
authorSimon Glass <sjg@chromium.org>
Thu, 6 Oct 2022 14:36:14 +0000 (08:36 -0600)
committerAnatolij Gustschin <agust@denx.de>
Sun, 30 Oct 2022 19:01:40 +0000 (20:01 +0100)
It is useful to be able to support multiple fonts. Add a function to
handle this as well as one to list the available fonts.

Signed-off-by: Simon Glass <sjg@chromium.org>
drivers/video/console_truetype.c
include/video_console.h

index 5fc737a..09421e7 100644 (file)
@@ -584,6 +584,16 @@ static struct font_info *console_truetype_find_font(void)
        return NULL;
 }
 
+void vidconsole_list_fonts(void)
+{
+       struct font_info *tab;
+
+       for (tab = font_table; tab->begin; tab++) {
+               if (abs(tab->begin - tab->end) > 4)
+                       printf("%s\n", tab->name);
+       }
+}
+
 /**
  * vidconsole_add_metrics() - Add a new font/size combination
  *
@@ -624,6 +634,30 @@ static int vidconsole_add_metrics(struct udevice *dev, const char *font_name,
        return priv->num_metrics++;
 }
 
+/**
+ * find_metrics() - Find the metrics for a given font and size
+ *
+ * @dev:       Video console device to update
+ * @name:      Name of font
+ * @size:      Size of the font (norminal pixel height)
+ * @return metrics, if found, else NULL
+ */
+static struct console_tt_metrics *find_metrics(struct udevice *dev,
+                                              const char *name, uint size)
+{
+       struct console_tt_priv *priv = dev_get_priv(dev);
+       int i;
+
+       for (i = 0; i < priv->num_metrics; i++) {
+               struct console_tt_metrics *met = &priv->metrics[i];
+
+               if (!strcmp(name, met->font_name) && met->font_size == size)
+                       return met;
+       }
+
+       return NULL;
+}
+
 static void select_metrics(struct udevice *dev, struct console_tt_metrics *met)
 {
        struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
@@ -640,6 +674,47 @@ static void select_metrics(struct udevice *dev, struct console_tt_metrics *met)
        vc_priv->tab_width_frac = VID_TO_POS(met->font_size) * 8 / 2;
 }
 
+int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
+{
+       struct console_tt_priv *priv = dev_get_priv(dev);
+       struct console_tt_metrics *met;
+       struct font_info *tab;
+
+       if (name || size) {
+               if (!size)
+                       size = CONFIG_CONSOLE_TRUETYPE_SIZE;
+               if (!name)
+                       name = priv->cur_met->font_name;
+
+               met = find_metrics(dev, name, size);
+               if (!met) {
+                       for (tab = font_table; tab->begin; tab++) {
+                               if (font_valid(tab) &&
+                                   !strcmp(name, tab->name)) {
+                                       int ret;
+
+                                       ret = vidconsole_add_metrics(dev,
+                                               tab->name, size, tab->begin);
+                                       if (ret < 0)
+                                               return log_msg_ret("add", ret);
+
+                                       met = &priv->metrics[ret];
+                                       break;
+                               }
+                       }
+               }
+               if (!met)
+                       return log_msg_ret("find", -ENOENT);
+       } else {
+               /* Use the default font */
+               met = priv->metrics;
+       }
+
+       select_metrics(dev, met);
+
+       return 0;
+}
+
 static int console_truetype_probe(struct udevice *dev)
 {
        struct console_tt_priv *priv = dev_get_priv(dev);
index 76c4b10..bef926c 100644 (file)
@@ -231,6 +231,22 @@ void vidconsole_position_cursor(struct udevice *dev, unsigned col,
  */
 void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y);
 
+/**
+ * vidconsole_list_fonts() - List the available fonts
+ *
+ * This shows a list on the console
+ */
+void vidconsole_list_fonts(void);
+
+/**
+ * vidconsole_select_font() - Select a font to use
+ *
+ * @dev: vidconsole device
+ * @name: Font name
+ * @size: Size of the font (norminal pixel height) or 0 for default
+ */
+int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
+
 #ifdef CONFIG_VIDEO_COPY
 /**
  * vidconsole_sync_copy() - Sync back to the copy framebuffer