[PATCH 12/16] video: Add a way to change the font name and size

Simon Glass sjg at chromium.org
Thu Oct 6 16:36:14 CEST 2022


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 at chromium.org>
---

 drivers/video/console_truetype.c | 75 ++++++++++++++++++++++++++++++++
 include/video_console.h          | 16 +++++++
 2 files changed, 91 insertions(+)

diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c
index 5fc737a9ff2..09421e7a905 100644
--- a/drivers/video/console_truetype.c
+++ b/drivers/video/console_truetype.c
@@ -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);
diff --git a/include/video_console.h b/include/video_console.h
index 76c4b10acf6..bef926cd433 100644
--- a/include/video_console.h
+++ b/include/video_console.h
@@ -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
-- 
2.38.0.rc1.362.ged0d419d3c-goog



More information about the U-Boot mailing list