[PATCH 14/16] video: Add commands to list and change fonts

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


Add a new 'font' command which allows the fonts to be listed as well as
selecting a different font and size.

Allow the test to run on sandbox, where multiple font/size combinations
are supported, as well as sandbox_flattree, where they are not.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 cmd/Makefile                     |  1 +
 cmd/font.c                       | 81 ++++++++++++++++++++++++++++++++
 configs/sandbox_defconfig        |  1 +
 doc/usage/cmd/font.rst           | 52 ++++++++++++++++++++
 doc/usage/index.rst              |  1 +
 drivers/video/console_truetype.c | 10 ++++
 include/test/suites.h            |  1 +
 include/video_console.h          |  9 ++++
 test/cmd/Makefile                |  1 +
 test/cmd/font.c                  | 77 ++++++++++++++++++++++++++++++
 test/cmd_ut.c                    |  6 +++
 11 files changed, 240 insertions(+)
 create mode 100644 cmd/font.c
 create mode 100644 doc/usage/cmd/font.rst
 create mode 100644 test/cmd/font.c

diff --git a/cmd/Makefile b/cmd/Makefile
index 86a77805f88..4288b3436ae 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_CMD_EXT2) += ext2.o
 obj-$(CONFIG_CMD_FAT) += fat.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
 obj-$(CONFIG_CMD_SQUASHFS) += sqfs.o
+obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
 obj-$(CONFIG_CMD_FLASH) += flash.o
 obj-$(CONFIG_CMD_FPGA) += fpga.o
 obj-$(CONFIG_CMD_FPGAD) += fpgad.o
diff --git a/cmd/font.c b/cmd/font.c
new file mode 100644
index 00000000000..3e522f3aaa1
--- /dev/null
+++ b/cmd/font.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * video commands
+ *
+ * Copyright 2022 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <video.h>
+#include <video_console.h>
+
+static int do_font_list(struct cmd_tbl *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	vidconsole_list_fonts();
+
+	return 0;
+}
+
+static int do_font_select(struct cmd_tbl *cmdtp, int flag, int argc,
+			  char *const argv[])
+{
+	struct udevice *dev;
+	const char *name;
+	uint size = 0;
+	int ret;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
+		return CMD_RET_FAILURE;
+	name = argv[1];
+	if (argc == 3)
+		size = dectoul(argv[2], NULL);
+	ret = vidconsole_select_font(dev, name, size);
+	if (ret) {
+		printf("Failed (error %d)\n", ret);
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+static int do_font_size(struct cmd_tbl *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	struct udevice *dev;
+	uint size;
+	int ret;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+
+	if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
+		return CMD_RET_FAILURE;
+
+	size = dectoul(argv[1], NULL);
+	ret = vidconsole_select_font(dev, NULL, size);
+	if (ret) {
+		printf("Failed (error %d)\n", ret);
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+
+
+#ifdef CONFIG_SYS_LONGHELP
+static char font_help_text[] =
+	"list       - list available fonts\n"
+	"font select <name> [<size>] - select font to use\n"
+	"font size <size> - select font size to";
+#endif
+
+U_BOOT_CMD_WITH_SUBCMDS(font, "Fonts", font_help_text,
+	U_BOOT_SUBCMD_MKENT(list, 1, 1, do_font_list),
+	U_BOOT_SUBCMD_MKENT(select, 3, 1, do_font_select),
+	U_BOOT_SUBCMD_MKENT(size, 2, 1, do_font_size));
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index df6a28ef249..4bb2a73ee60 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -293,6 +293,7 @@ CONFIG_DM_VIDEO=y
 CONFIG_VIDEO_COPY=y
 CONFIG_CONSOLE_ROTATION=y
 CONFIG_CONSOLE_TRUETYPE=y
+CONFIG_CONSOLE_TRUETYPE_MAX_METRICS=10
 CONFIG_CONSOLE_TRUETYPE_CANTORAONE=y
 CONFIG_I2C_EDID=y
 CONFIG_VIDEO_SANDBOX_SDL=y
diff --git a/doc/usage/cmd/font.rst b/doc/usage/cmd/font.rst
new file mode 100644
index 00000000000..6fb08232703
--- /dev/null
+++ b/doc/usage/cmd/font.rst
@@ -0,0 +1,52 @@
+.. SPDX-License-Identifier: GPL-2.0+:
+
+font command
+============
+
+Synopis
+-------
+
+::
+
+    font list
+    font select <name> [<size>]
+    font size <size>
+
+
+Description
+-----------
+
+The *font* command allows selection of the font to use on the video console.
+This is available when the Truetype console is in use. This is the case when
+`CONFIG_CONSOLE_TRUETYPE` is enabled.
+
+
+font list
+~~~~~~~~~
+
+This lists the available fonts, using the name of the font file in the build.
+
+
+font select
+~~~~~~~~~~~
+
+This selects a new font and optionally changes the size.
+
+
+font size
+~~~~~~~~~
+
+This changes the font size only.
+
+
+Examples
+--------
+
+::
+
+    => font list
+    nimbus_sans_l_regular
+    cantoraone_regular
+    => font size 40
+    => font select cantoraone_regular 20
+    =>
diff --git a/doc/usage/index.rst b/doc/usage/index.rst
index 0fda121026e..483a7dcbf86 100644
--- a/doc/usage/index.rst
+++ b/doc/usage/index.rst
@@ -46,6 +46,7 @@ Shell commands
    cmd/fatinfo
    cmd/fatload
    cmd/fdt
+   cmd/font
    cmd/for
    cmd/gpio
    cmd/load
diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c
index 09421e7a905..6859c9fa116 100644
--- a/drivers/video/console_truetype.c
+++ b/drivers/video/console_truetype.c
@@ -715,6 +715,16 @@ int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
 	return 0;
 }
 
+const char *vidconsole_get_font(struct udevice *dev, uint *sizep)
+{
+	struct console_tt_priv *priv = dev_get_priv(dev);
+	struct console_tt_metrics *met = priv->cur_met;
+
+	*sizep = met->font_size;
+
+	return met->font_name;
+}
+
 static int console_truetype_probe(struct udevice *dev)
 {
 	struct console_tt_priv *priv = dev_get_priv(dev);
diff --git a/include/test/suites.h b/include/test/suites.h
index 44025ccecd6..a01000e127b 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -39,6 +39,7 @@ int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc,
 int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
+int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
diff --git a/include/video_console.h b/include/video_console.h
index bef926cd433..d755eb73cf2 100644
--- a/include/video_console.h
+++ b/include/video_console.h
@@ -247,6 +247,15 @@ void vidconsole_list_fonts(void);
  */
 int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
 
+/**
+ * vidconsole_get_font() - get the current font name and size
+ *
+ * @dev: vidconsole device
+ * @sizep: Place to put the font size (nominal height in pixels)
+ * Returns: Current font name
+ */
+const char *vidconsole_get_font(struct udevice *dev, uint *sizep);
+
 #ifdef CONFIG_VIDEO_COPY
 /**
  * vidconsole_sync_copy() - Sync back to the copy framebuffer
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index 1bb02d93a23..50375f7cf65 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -11,6 +11,7 @@ endif
 obj-y += mem.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
+obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
 obj-$(CONFIG_CMD_LOADM) += loadm.o
 obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
 obj-$(CONFIG_CMD_PINMUX) += pinmux.o
diff --git a/test/cmd/font.c b/test/cmd/font.c
new file mode 100644
index 00000000000..7a4156ade62
--- /dev/null
+++ b/test/cmd/font.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for font command
+ *
+ * Copyright 2022 Google LLC
+ */
+
+#include <common.h>
+#include <console.h>
+#include <dm.h>
+#include <video_console.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+/* Declare a new fdt test */
+#define FONT_TEST(_name, _flags)	UNIT_TEST(_name, _flags, font_test)
+
+/* Test 'fdt addr' resizing an fdt */
+static int font_test_base(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+	int max_metrics;
+	uint size;
+	int ret;
+
+	ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
+	ut_assertok(uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev));
+
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_command("font list", 0));
+	ut_assert_nextline("nimbus_sans_l_regular");
+	ut_assert_nextline("cantoraone_regular");
+	ut_assertok(ut_check_console_end(uts));
+
+	ut_asserteq_str("nimbus_sans_l_regular",
+			vidconsole_get_font(dev, &size));
+	ut_asserteq(18, size);
+
+	max_metrics = 1;
+	if (IS_ENABLED(CONFIG_CONSOLE_TRUETYPE))
+		max_metrics = IF_ENABLED_INT(CONFIG_CONSOLE_TRUETYPE,
+				     CONFIG_CONSOLE_TRUETYPE_MAX_METRICS);
+
+	ret = run_command("font select cantoraone_regular 40", 0);
+	if (max_metrics < 2) {
+		ut_asserteq(1, ret);
+		ut_assert_nextline("Failed (error -7)");
+		ut_assertok(ut_check_console_end(uts));
+		return 0;
+	}
+
+	ut_assertok(ret);
+	ut_assertok(ut_check_console_end(uts));
+
+	ut_asserteq_str("cantoraone_regular",
+			vidconsole_get_font(dev, &size));
+	ut_asserteq(40, size);
+
+	ut_assertok(run_command("font size 30", 0));
+	ut_assertok(ut_check_console_end(uts));
+
+	ut_asserteq_str("cantoraone_regular",
+			vidconsole_get_font(dev, &size));
+	ut_asserteq(30, size);
+
+	return 0;
+}
+FONT_TEST(font_test_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT |
+	  UT_TESTF_CONSOLE_REC | UT_TESTF_DM);
+
+int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = UNIT_TEST_SUITE_START(font_Test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(font_test);
+
+	return cmd_ut_category("font", "font_test_", tests, n_ents, argc, argv);
+}
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 11c219b48ac..100bf869c26 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -49,6 +49,9 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #ifdef CONFIG_CMD_FDT
 	U_BOOT_CMD_MKENT(fdt, CONFIG_SYS_MAXARGS, 1, do_ut_fdt, "", ""),
 #endif
+#ifdef CONFIG_CONSOLE_TRUETYPE
+	U_BOOT_CMD_MKENT(font, CONFIG_SYS_MAXARGS, 1, do_ut_font, "", ""),
+#endif
 #ifdef CONFIG_UT_OPTEE
 	U_BOOT_CMD_MKENT(optee, CONFIG_SYS_MAXARGS, 1, do_ut_optee, "", ""),
 #endif
@@ -144,6 +147,9 @@ static char ut_help_text[] =
 #ifdef CONFIG_CMD_FDT
 	"ut fdt [test-name] - test of the fdt command\n"
 #endif
+#ifdef CONFIG_CONSOLE_TRUETYPE
+	"ut font [test-name] - test of the font command\n"
+#endif
 #ifdef CONFIG_UT_LIB
 	"ut lib [test-name] - test library functions\n"
 #endif
-- 
2.38.0.rc1.362.ged0d419d3c-goog



More information about the U-Boot mailing list