33 * @author Martin Pulec <[email protected] > 44 */
55/*
6- * Copyright (c) 2014-2023 CESNET, z. s. p. o.
6+ * Copyright (c) 2014-2025 CESNET, zájmové sdružení právnických osob
77 * All rights reserved.
88 *
99 * Redistribution and use in source and binary forms, with or without
@@ -266,6 +266,42 @@ static bool draw_line_init(unsigned char *out) {
266266 return true;
267267}
268268
269+ static void
270+ draw_letter (char c , uint32_t color , char * buf , const unsigned char * font_data ,
271+ int pitch , bool solid )
272+ {
273+ if (c < ' ' || c > '~' ) {
274+ c = '?' ;
275+ }
276+ c -= ' ' ;
277+ for (int j = 0 ; j < FONT_H ; ++ j ) {
278+ for (int i = 0 ; i < FONT_W ; ++ i ) {
279+ int pos_x = (FONT_W * c + i ) / 8 ;
280+ int mask = 1 << (FONT_W - ((FONT_W * c + i ) % 8 ));
281+ int offset = (FONT_W * FONT_COUNT + 7 ) / 8 * j ;
282+ if (font_data [offset + pos_x ] & mask ) {
283+ // clang-format off
284+ buf [(j * pitch ) + (4 * i )] = color & 0xFFU ;
285+ buf [(j * pitch ) + (4 * i ) + 1 ] = (color >> 8U ) & 0xFFU ;
286+ buf [(j * pitch ) + (4 * i ) + 2 ] = (color >> 16U ) & 0xFFU ;
287+ buf [(j * pitch ) + (4 * i ) + 3 ] = (color >> 24U ) & 0xFFU ;
288+ // clang-format on
289+ } else if (solid ) {
290+ buf [(j * pitch ) + (4 * i )] = 0 ;
291+ buf [(j * pitch ) + (4 * i ) + 1 ] = 0 ;
292+ buf [(j * pitch ) + (4 * i ) + 2 ] = 0 ;
293+ buf [(j * pitch ) + (4 * i ) + 3 ] = 0xFFU ; // alpha
294+ }
295+ }
296+ if (solid ) { // fill space between characters
297+ buf [(j * pitch ) + (4 * (FONT_W_SPACE - 1 ))] = 0 ;
298+ buf [(j * pitch ) + (4 * (FONT_W_SPACE - 1 )) + 1 ] = 0 ;
299+ buf [(j * pitch ) + (4 * (FONT_W_SPACE - 1 )) + 2 ] = 0 ;
300+ buf [(j * pitch ) + (4 * (FONT_W_SPACE - 1 )) + 3 ] = 0xFFU ;
301+ }
302+ }
303+ }
304+
269305/**
270306 * draws a line with built-in bitmap 12x7 bitmap font separated by 1 px space, RGBA
271307 */
@@ -279,43 +315,13 @@ bool draw_line(char *buf, int pitch, const char *text, uint32_t color, bool soli
279315 font_data_initialized = true;
280316 }
281317 int idx = 0 ;
282- enum {
283- WIDTH = FONT_W + 1 , ///< adding 1 pix space between letters
284- };
285318 while (* text ) {
286- char c = * text ;
287- if (c < ' ' || c > '~' ) {
288- c = '?' ;
289- }
290- c -= ' ' ;
291- for (int j = 0 ; j < FONT_H ; ++ j ) {
292- for (int i = 0 ; i < FONT_W ; ++ i ) {
293- int pos_x = (FONT_W * c + i ) / 8 ;
294- int mask = 1 << (FONT_W - ((FONT_W * c + i ) % 8 ));
295- int offset = (FONT_W * FONT_COUNT + 7 ) / 8 * j ;
296- if (font_data [offset + pos_x ] & mask ) {
297- buf [j * pitch + 4 * (i + idx * WIDTH )] = color & 0xFFU ;
298- buf [j * pitch + 4 * (i + idx * WIDTH ) + 1 ] = (color >> 8U ) & 0xFFU ;
299- buf [j * pitch + 4 * (i + idx * WIDTH ) + 2 ] = (color >> 16U ) & 0xFFU ;
300- buf [j * pitch + 4 * (i + idx * WIDTH ) + 3 ] = (color >> 24U ) & 0xFFU ;
301- } else if (solid ) {
302- buf [j * pitch + 4 * (i + idx * WIDTH )] =
303- buf [j * pitch + 4 * (i + idx * WIDTH ) + 1 ] =
304- buf [j * pitch + 4 * (i + idx * WIDTH ) + 2 ] = 0 ;
305- buf [j * pitch + 4 * (i + idx * WIDTH ) + 3 ] = 0xFFU ;
306- }
307- }
308- if (solid ) { // fill space between characters
309- buf [j * pitch + 4 * ((WIDTH - 1 ) + idx * WIDTH )] =
310- buf [j * pitch + 4 * ((WIDTH - 1 ) + idx * WIDTH ) + 1 ] =
311- buf [j * pitch + 4 * ((WIDTH - 1 ) + idx * WIDTH ) + 2 ] = 0 ;
312- buf [j * pitch + 4 * ((WIDTH - 1 ) + idx * WIDTH ) + 3 ] = 0xFFU ;
313- }
314- }
315- if ((++ idx + 1 ) * WIDTH * 4 > pitch ) {
319+ char c = * text ++ ;
320+ draw_letter (c , color , buf + (size_t ) (4 * idx * FONT_W_SPACE ),
321+ font_data , pitch , solid );
322+ if ((++ idx + 1 ) * FONT_W_SPACE * 4 > pitch ) {
316323 return true;
317324 }
318- ++ text ;
319325 }
320326
321327 return true;
0 commit comments