1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Google, Inc
11 #include <video_console.h>
13 /* Functions needed by stb_truetype.h */
14 static int tt_floor(double val)
17 return (int)(val - 0.999);
22 static int tt_ceil(double val)
27 return (int)(val + 0.999);
30 static double frac(double val)
32 return val - tt_floor(val);
35 static double tt_fabs(double x)
37 return x < 0 ? -x : x;
41 * Simple square root algorithm. This is from:
42 * http://stackoverflow.com/questions/1623375/writing-your-own-square-root-function
43 * Written by Chihung Yu
44 * Creative Commons license
45 * http://creativecommons.org/licenses/by-sa/3.0/legalcode
46 * It has been modified to compile correctly, and for U-Boot style.
48 static double tt_sqrt(double value)
53 while (hi - lo > 0.00001) {
54 double mid = lo + (hi - lo) / 2;
56 if (mid * mid - value > 0.00001)
65 #define STBTT_ifloor tt_floor
66 #define STBTT_iceil tt_ceil
67 #define STBTT_fabs tt_fabs
68 #define STBTT_sqrt tt_sqrt
69 #define STBTT_malloc(size, u) ((void)(u), malloc(size))
70 #define STBTT_free(size, u) ((void)(u), free(size))
71 #define STBTT_assert(x)
72 #define STBTT_strlen(x) strlen(x)
73 #define STBTT_memcpy memcpy
74 #define STBTT_memset memset
76 #define STB_TRUETYPE_IMPLEMENTATION
77 #include "stb_truetype.h"
80 * struct pos_info - Records a cursor position
82 * @xpos_frac: Fractional X position in pixels (multiplied by VID_FRAC_DIV)
83 * @ypos: Y position (pixels from the top)
91 * Allow one for each character on the command line plus one for each newline.
92 * This is just an estimate, but it should not be exceeded.
94 #define POS_HISTORY_SIZE (CONFIG_SYS_CBSIZE * 11 / 10)
97 * struct console_tt_priv - Private data for this driver
99 * @font_size: Vertical font size in pixels
100 * @font_data: Pointer to TrueType font file contents
101 * @font: TrueType font information for the current font
102 * @pos: List of cursor positions for each character written. This is
103 * used to handle backspace. We clear the frame buffer between
104 * the last position and the current position, thus erasing the
105 * last character. We record enough characters to go back to the
106 * start of the current command line.
107 * @pos_ptr: Current position in the position history
108 * @baseline: Pixel offset of the font's baseline from the cursor position.
109 * This is the 'ascent' of the font, scaled to pixel coordinates.
110 * It measures the distance from the baseline to the top of the
112 * @scale: Scale of the font. This is calculated from the pixel height
113 * of the font. It is used by the STB library to generate images
114 * of the correct size.
116 struct console_tt_priv {
120 struct pos_info pos[POS_HISTORY_SIZE];
126 static int console_truetype_set_row(struct udevice *dev, uint row, int clr)
128 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
129 struct console_tt_priv *priv = dev_get_priv(dev);
133 line = vid_priv->fb + row * priv->font_size * vid_priv->line_length;
134 end = line + priv->font_size * vid_priv->line_length;
136 switch (vid_priv->bpix) {
137 #ifdef CONFIG_VIDEO_BPP8
141 for (dst = line; dst < (u8 *)end; ++dst)
146 #ifdef CONFIG_VIDEO_BPP16
150 for (dst = line; dst < (u16 *)end; ++dst)
155 #ifdef CONFIG_VIDEO_BPP32
159 for (dst = line; dst < (u32 *)end; ++dst)
167 ret = vidconsole_sync_copy(dev, line, end);
174 static int console_truetype_move_rows(struct udevice *dev, uint rowdst,
175 uint rowsrc, uint count)
177 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
178 struct console_tt_priv *priv = dev_get_priv(dev);
183 dst = vid_priv->fb + rowdst * priv->font_size * vid_priv->line_length;
184 src = vid_priv->fb + rowsrc * priv->font_size * vid_priv->line_length;
185 ret = vidconsole_memmove(dev, dst, src, priv->font_size *
186 vid_priv->line_length * count);
190 /* Scroll up our position history */
191 diff = (rowsrc - rowdst) * priv->font_size;
192 for (i = 0; i < priv->pos_ptr; i++)
193 priv->pos[i].ypos -= diff;
198 static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y,
201 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
202 struct udevice *vid = dev->parent;
203 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
204 struct console_tt_priv *priv = dev_get_priv(dev);
205 stbtt_fontinfo *font = &priv->font;
206 int width, height, xoff, yoff;
207 double xpos, x_shift;
209 int width_frac, linenum;
210 struct pos_info *pos;
213 void *start, *end, *line;
216 /* First get some basic metrics about this character */
217 stbtt_GetCodepointHMetrics(font, ch, &advance, &lsb);
220 * First out our current X position in fractional pixels. If we wrote
221 * a character previously, using kerning to fine-tune the position of
223 xpos = frac(VID_TO_PIXEL((double)x));
224 if (vc_priv->last_ch) {
225 xpos += priv->scale * stbtt_GetCodepointKernAdvance(font,
226 vc_priv->last_ch, ch);
230 * Figure out where the cursor will move to after this character, and
231 * abort if we are out of space on this line. Also calculate the
232 * effective width of this character, which will be our return value:
233 * it dictates how much the cursor will move forward on the line.
235 x_shift = xpos - (double)tt_floor(xpos);
236 xpos += advance * priv->scale;
237 width_frac = (int)VID_TO_POS(xpos);
238 if (x + width_frac >= vc_priv->xsize_frac)
241 /* Write the current cursor position into history */
242 if (priv->pos_ptr < POS_HISTORY_SIZE) {
243 pos = &priv->pos[priv->pos_ptr];
244 pos->xpos_frac = vc_priv->xcur_frac;
245 pos->ypos = vc_priv->ycur;
250 * Figure out how much past the start of a pixel we are, and pass this
251 * information into the render, which will return a 8-bit-per-pixel
252 * image of the character. For empty characters, like ' ', data will
255 data = stbtt_GetCodepointBitmapSubpixel(font, priv->scale, priv->scale,
256 x_shift, 0, ch, &width, &height,
261 /* Figure out where to write the character in the frame buffer */
263 start = vid_priv->fb + y * vid_priv->line_length +
264 VID_TO_PIXEL(x) * VNBYTES(vid_priv->bpix);
265 linenum = priv->baseline + yoff;
267 start += linenum * vid_priv->line_length;
271 * Write a row at a time, converting the 8bpp image into the colour
272 * depth of the display. We only expect white-on-black or the reverse
273 * so the code only handles this simple case.
275 for (row = 0; row < height; row++) {
276 switch (vid_priv->bpix) {
278 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
279 u8 *dst = line + xoff;
282 for (i = 0; i < width; i++) {
286 if (vid_priv->colour_bg)
289 if (vid_priv->colour_fg)
298 #ifdef CONFIG_VIDEO_BPP16
300 uint16_t *dst = (uint16_t *)line + xoff;
303 for (i = 0; i < width; i++) {
307 if (vid_priv->colour_bg)
312 if (vid_priv->colour_fg)
322 #ifdef CONFIG_VIDEO_BPP32
324 u32 *dst = (u32 *)line + xoff;
327 for (i = 0; i < width; i++) {
331 if (vid_priv->colour_bg)
333 out = val | val << 8 | val << 16;
334 if (vid_priv->colour_fg)
349 line += vid_priv->line_length;
351 ret = vidconsole_sync_copy(dev, start, line);
360 * console_truetype_erase() - Erase a character
362 * This is used for backspace. We erase a square of the display within the
365 * @dev: Device to update
366 * @xstart: X start position in pixels from the left
367 * @ystart: Y start position in pixels from the top
368 * @xend: X end position in pixels from the left
369 * @yend: Y end position in pixels from the top
370 * @clr: Value to write
371 * @return 0 if OK, -ENOSYS if the display depth is not supported
373 static int console_truetype_erase(struct udevice *dev, int xstart, int ystart,
374 int xend, int yend, int clr)
376 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
378 int pixels = xend - xstart;
381 start = vid_priv->fb + ystart * vid_priv->line_length;
382 start += xstart * VNBYTES(vid_priv->bpix);
384 for (row = ystart; row < yend; row++) {
385 switch (vid_priv->bpix) {
386 #ifdef CONFIG_VIDEO_BPP8
390 for (i = 0; i < pixels; i++)
395 #ifdef CONFIG_VIDEO_BPP16
397 uint16_t *dst = line;
399 for (i = 0; i < pixels; i++)
404 #ifdef CONFIG_VIDEO_BPP32
406 uint32_t *dst = line;
408 for (i = 0; i < pixels; i++)
416 line += vid_priv->line_length;
418 ret = vidconsole_sync_copy(dev, start, line);
426 * console_truetype_backspace() - Handle a backspace operation
428 * This clears the previous character so that the console looks as if it had
431 * @dev: Device to update
432 * @return 0 if OK, -ENOSYS if not supported
434 static int console_truetype_backspace(struct udevice *dev)
436 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
437 struct console_tt_priv *priv = dev_get_priv(dev);
438 struct udevice *vid_dev = dev->parent;
439 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
440 struct pos_info *pos;
444 * This indicates a very strange error higher in the stack. The caller
445 * has sent out n character and n + 1 backspaces.
450 /* Pop the last cursor position off the stack */
451 pos = &priv->pos[--priv->pos_ptr];
454 * Figure out the end position for clearing. Normally it is the current
455 * cursor position, but if we are clearing a character on the previous
456 * line, we clear from the end of the line.
458 if (pos->ypos == vc_priv->ycur)
459 xend = VID_TO_PIXEL(vc_priv->xcur_frac);
461 xend = vid_priv->xsize;
463 console_truetype_erase(dev, VID_TO_PIXEL(pos->xpos_frac), pos->ypos,
464 xend, pos->ypos + vc_priv->y_charsize,
465 vid_priv->colour_bg);
467 /* Move the cursor back to where it was when we pushed this record */
468 vc_priv->xcur_frac = pos->xpos_frac;
469 vc_priv->ycur = pos->ypos;
474 static int console_truetype_entry_start(struct udevice *dev)
476 struct console_tt_priv *priv = dev_get_priv(dev);
478 /* A new input line has start, so clear our history */
485 * Provides a list of fonts which can be obtained at run-time in U-Boot. These
486 * are compiled in by the Makefile.
488 * At present there is no mechanism to select a particular font - the first
489 * one found is the one that is used. But the build system and the code here
490 * supports multiple fonts, which may be useful for certain firmware screens.
498 #define FONT_DECL(_name) \
499 extern u8 __ttf_ ## _name ## _begin[]; \
500 extern u8 __ttf_ ## _name ## _end[];
502 #define FONT_ENTRY(_name) { \
504 .begin = __ttf_ ## _name ## _begin, \
505 .end = __ttf_ ## _name ## _end, \
508 FONT_DECL(nimbus_sans_l_regular);
509 FONT_DECL(ankacoder_c75_r);
510 FONT_DECL(rufscript010);
511 FONT_DECL(cantoraone_regular);
513 static struct font_info font_table[] = {
514 #ifdef CONFIG_CONSOLE_TRUETYPE_NIMBUS
515 FONT_ENTRY(nimbus_sans_l_regular),
517 #ifdef CONFIG_CONSOLE_TRUETYPE_ANKACODER
518 FONT_ENTRY(ankacoder_c75_r),
520 #ifdef CONFIG_CONSOLE_TRUETYPE_RUFSCRIPT
521 FONT_ENTRY(rufscript010),
523 #ifdef CONFIG_CONSOLE_TRUETYPE_CANTORAONE
524 FONT_ENTRY(cantoraone_regular),
529 #define FONT_BEGIN(name) __ttf_ ## name ## _begin
530 #define FONT_END(name) __ttf_ ## name ## _end
531 #define FONT_IS_VALID(name) (abs(FONT_END(name) - FONT_BEGIN) > 4)
534 * console_truetype_find_font() - Find a suitable font
536 * This searched for the first available font.
538 * @return pointer to the font, or NULL if none is found
540 static u8 *console_truetype_find_font(void)
542 struct font_info *tab;
544 for (tab = font_table; tab->begin; tab++) {
545 if (abs(tab->begin - tab->end) > 4) {
546 debug("%s: Font '%s', at %p, size %lx\n", __func__,
547 tab->name, tab->begin,
548 (ulong)(tab->end - tab->begin));
556 static int console_truetype_probe(struct udevice *dev)
558 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
559 struct console_tt_priv *priv = dev_get_priv(dev);
560 struct udevice *vid_dev = dev->parent;
561 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
562 stbtt_fontinfo *font = &priv->font;
565 debug("%s: start\n", __func__);
566 if (vid_priv->font_size)
567 priv->font_size = vid_priv->font_size;
569 priv->font_size = CONFIG_CONSOLE_TRUETYPE_SIZE;
570 priv->font_data = console_truetype_find_font();
571 if (!priv->font_data) {
572 debug("%s: Could not find any fonts\n", __func__);
576 vc_priv->x_charsize = priv->font_size;
577 vc_priv->y_charsize = priv->font_size;
578 vc_priv->xstart_frac = VID_TO_POS(2);
579 vc_priv->cols = vid_priv->xsize / priv->font_size;
580 vc_priv->rows = vid_priv->ysize / priv->font_size;
581 vc_priv->tab_width_frac = VID_TO_POS(priv->font_size) * 8 / 2;
583 if (!stbtt_InitFont(font, priv->font_data, 0)) {
584 debug("%s: Font init failed\n", __func__);
588 /* Pre-calculate some things we will need regularly */
589 priv->scale = stbtt_ScaleForPixelHeight(font, priv->font_size);
590 stbtt_GetFontVMetrics(font, &ascent, 0, 0);
591 priv->baseline = (int)(ascent * priv->scale);
592 debug("%s: ready\n", __func__);
597 struct vidconsole_ops console_truetype_ops = {
598 .putc_xy = console_truetype_putc_xy,
599 .move_rows = console_truetype_move_rows,
600 .set_row = console_truetype_set_row,
601 .backspace = console_truetype_backspace,
602 .entry_start = console_truetype_entry_start,
605 U_BOOT_DRIVER(vidconsole_truetype) = {
606 .name = "vidconsole_tt",
607 .id = UCLASS_VIDEO_CONSOLE,
608 .ops = &console_truetype_ops,
609 .probe = console_truetype_probe,
610 .priv_auto = sizeof(struct console_tt_priv),