1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
5 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
6 * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
10 #include <video_console.h>
12 #include <video_font.h>
13 #include "vidconsole_internal.h"
16 * console_set_font() - prepare vidconsole for chosen font.
18 * @dev vidconsole device
19 * @fontdata pointer to font data struct
21 static int console_set_font(struct udevice *dev, struct video_fontdata *fontdata)
23 struct console_simple_priv *priv = dev_get_priv(dev);
24 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
25 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
27 debug("console_simple: setting %s font\n", fontdata->name);
28 debug("width: %d\n", fontdata->width);
29 debug("byte width: %d\n", fontdata->byte_width);
30 debug("height: %d\n", fontdata->height);
32 priv->fontdata = fontdata;
33 vc_priv->x_charsize = fontdata->width;
34 vc_priv->y_charsize = fontdata->height;
35 if (vid_priv->rot % 2) {
36 vc_priv->cols = vid_priv->ysize / fontdata->width;
37 vc_priv->rows = vid_priv->xsize / fontdata->height;
38 vc_priv->xsize_frac = VID_TO_POS(vid_priv->ysize);
40 vc_priv->cols = vid_priv->xsize / fontdata->width;
41 vc_priv->rows = vid_priv->ysize / fontdata->height;
47 int check_bpix_support(int bpix)
49 if (bpix == VIDEO_BPP8 && CONFIG_IS_ENABLED(VIDEO_BPP8))
51 else if (bpix == VIDEO_BPP16 && CONFIG_IS_ENABLED(VIDEO_BPP16))
53 else if (bpix == VIDEO_BPP32 && CONFIG_IS_ENABLED(VIDEO_BPP32))
59 inline void fill_pixel_and_goto_next(void **dstp, u32 value, int pbytes, int step)
75 *dstp = dst_byte + step;
78 int fill_char_vertically(uchar *pfont, void **line, struct video_priv *vid_priv,
79 struct video_fontdata *fontdata, bool direction)
81 int step, line_step, pbytes, bitcount, width_remainder, ret;
84 ret = check_bpix_support(vid_priv->bpix);
88 pbytes = VNBYTES(vid_priv->bpix);
91 line_step = -vid_priv->line_length;
94 line_step = vid_priv->line_length;
97 width_remainder = fontdata->width % 8;
98 for (int row = 0; row < fontdata->height; row++) {
103 for (int col = 0; col < fontdata->byte_width; col++) {
104 if (width_remainder) {
105 bool is_last_col = (fontdata->byte_width - col == 1);
108 bitcount = width_remainder;
112 for (int bit = 0; bit < bitcount; bit++) {
113 u32 value = (bits & 0x80) ?
114 vid_priv->colour_fg :
117 fill_pixel_and_goto_next(&dst,
126 pfont += fontdata->byte_width;
131 int fill_char_horizontally(uchar *pfont, void **line, struct video_priv *vid_priv,
132 struct video_fontdata *fontdata, bool direction)
134 int step, line_step, pbytes, bitcount = 8, width_remainder, ret;
138 ret = check_bpix_support(vid_priv->bpix);
142 pbytes = VNBYTES(vid_priv->bpix);
145 line_step = vid_priv->line_length;
148 line_step = -vid_priv->line_length;
151 width_remainder = fontdata->width % 8;
152 for (int col = 0; col < fontdata->byte_width; col++) {
154 if (width_remainder) {
155 bool is_last_col = (fontdata->byte_width - col == 1);
158 bitcount = width_remainder;
160 for (int bit = 0; bit < bitcount; bit++) {
162 for (int row = 0; row < fontdata->height; row++) {
163 u32 value = (pfont[row * fontdata->byte_width + col]
164 & mask) ? vid_priv->colour_fg : vid_priv->colour_bg;
166 fill_pixel_and_goto_next(&dst,
179 int console_probe(struct udevice *dev)
181 return console_set_font(dev, fonts);
184 const char *console_simple_get_font_size(struct udevice *dev, uint *sizep)
186 struct console_simple_priv *priv = dev_get_priv(dev);
188 *sizep = priv->fontdata->width;
190 return priv->fontdata->name;
193 int console_simple_get_font(struct udevice *dev, int seq, struct vidfont_info *info)
195 info->name = fonts[seq].name;
200 int console_simple_select_font(struct udevice *dev, const char *name, uint size)
202 struct video_fontdata *font;
206 console_set_font(dev, fonts);
210 for (font = fonts; font->name; font++) {
211 if (!strcmp(name, font->name)) {
212 console_set_font(dev, font);
216 printf("no such font: %s, make sure it's name has <width>x<height> format\n", name);