aboutsummaryrefslogtreecommitdiff
path: root/drivers/video
diff options
context:
space:
mode:
authorSimon Glass2023-06-01 10:22:46 -0600
committerTom Rini2023-07-14 12:54:51 -0400
commitb828ed7d79295cfebcb0f958f26a33664fae045c (patch)
tree8558905b0d6c53146302e7d9c459c1b81b22728b /drivers/video
parent648a4991d0713af656a2fa50ec8e708c2beb044e (diff)
console: Allow measuring the bounding box of text
For laying out text accurately it is necessary to know the width and height of the text. Add a measure() method to the console API, so this can be supported. Add an implementation for truetype and a base implementation for the normal console. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'drivers/video')
-rw-r--r--drivers/video/console_truetype.c64
-rw-r--r--drivers/video/vidconsole-uclass.c22
2 files changed, 84 insertions, 2 deletions
diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c
index 6f3fc82f9b0..288123a2e06 100644
--- a/drivers/video/console_truetype.c
+++ b/drivers/video/console_truetype.c
@@ -614,8 +614,8 @@ 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;
}
-static int truetype_select_font(struct udevice *dev, const char *name,
- uint size)
+static int get_metrics(struct udevice *dev, const char *name, uint size,
+ struct console_tt_metrics **metp)
{
struct console_tt_priv *priv = dev_get_priv(dev);
struct console_tt_metrics *met;
@@ -653,11 +653,70 @@ static int truetype_select_font(struct udevice *dev, const char *name,
met = priv->metrics;
}
+ *metp = met;
+
+ return 0;
+}
+
+static int truetype_select_font(struct udevice *dev, const char *name,
+ uint size)
+{
+ struct console_tt_metrics *met;
+ int ret;
+
+ ret = get_metrics(dev, name, size, &met);
+ if (ret)
+ return log_msg_ret("sel", ret);
+
select_metrics(dev, met);
return 0;
}
+int truetype_measure(struct udevice *dev, const char *name, uint size,
+ const char *text, struct vidconsole_bbox *bbox)
+{
+ struct console_tt_metrics *met;
+ stbtt_fontinfo *font;
+ int lsb, advance;
+ const char *s;
+ int width;
+ int last;
+ int ret;
+
+ ret = get_metrics(dev, name, size, &met);
+ if (ret)
+ return log_msg_ret("sel", ret);
+
+ bbox->valid = false;
+ if (!*text)
+ return 0;
+
+ font = &met->font;
+ width = 0;
+ for (last = 0, s = text; *s; s++) {
+ int ch = *s;
+
+ /* Used kerning to fine-tune the position of this character */
+ if (last)
+ width += stbtt_GetCodepointKernAdvance(font, last, ch);
+
+ /* First get some basic metrics about this character */
+ stbtt_GetCodepointHMetrics(font, ch, &advance, &lsb);
+
+ width += advance;
+ last = ch;
+ }
+
+ bbox->valid = true;
+ bbox->x0 = 0;
+ bbox->y0 = 0;
+ bbox->x1 = tt_ceil((double)width * met->scale);
+ bbox->y1 = met->font_size;
+
+ return 0;
+}
+
const char *console_truetype_get_font_size(struct udevice *dev, uint *sizep)
{
struct console_tt_priv *priv = dev_get_priv(dev);
@@ -709,6 +768,7 @@ struct vidconsole_ops console_truetype_ops = {
.get_font = console_truetype_get_font,
.get_font_size = console_truetype_get_font_size,
.select_font = truetype_select_font,
+ .measure = truetype_measure,
};
U_BOOT_DRIVER(vidconsole_truetype) = {
diff --git a/drivers/video/vidconsole-uclass.c b/drivers/video/vidconsole-uclass.c
index 3f89537c47b..05f93047809 100644
--- a/drivers/video/vidconsole-uclass.c
+++ b/drivers/video/vidconsole-uclass.c
@@ -596,6 +596,28 @@ int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
return ops->select_font(dev, name, size);
}
+int vidconsole_measure(struct udevice *dev, const char *name, uint size,
+ const char *text, struct vidconsole_bbox *bbox)
+{
+ struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
+ struct vidconsole_ops *ops = vidconsole_get_ops(dev);
+ int ret;
+
+ if (ops->select_font) {
+ ret = ops->measure(dev, name, size, text, bbox);
+ if (ret != -ENOSYS)
+ return ret;
+ }
+
+ bbox->valid = true;
+ bbox->x0 = 0;
+ bbox->y0 = 0;
+ bbox->x1 = priv->x_charsize * strlen(text);
+ bbox->y1 = priv->y_charsize;
+
+ return 0;
+}
+
void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
enum colour_idx bg, struct vidconsole_colour *old)
{