GlyType 0.1
a ultra lightweight font renderer and font
Loading...
Searching...
No Matches
gly_type_render.h File Reference

gly_type_render.h More...

Macros

#define GLY_TYPE_INT   unsigned char
 
#define GLY_TYPE_SAFE
 

Functions

void gly_type_render (GLY_TYPE_INT x, GLY_TYPE_INT y, GLY_TYPE_INT s, const char *t, signed int len, const void *const f)
 

Detailed Description

a ultra lightweight font renderer and font

Date
2024
Version
0.1
Author
RodrigoDornelles
Style
The font style is inspired by digital displays, but not outdated, with its straight lines and some diagonals, a modernized look, and also a nice differentiation between numbers and characters.
7 Segments
The renderer stores the glyphs as if it were a 7-segment display, thus spending only 1 bit for each line, which is the trick of being so compact.

The 8 bit/segment it is a special line, its size varies, from bottom to top it stops when it hits the first fixed segment G or A There are special characters, which would be x , v and z to differentiate them from all other characters, they have their own internal rendering function, are flagged as segment H without segment G or A

FAAAAAAAAAAAAB
FFAAAAAAAAABBB
FFF........BBB
FFF........BBB
FFGGGGGGGGGGBB
EEGGGGGGGGGGCC
EEE........CCC
EEE........CCC
EEDDDDDDDDDDCC
EDDDDDDDDDDDDC
Source Code
#ifndef H_GLY_TYPE_RENDER
#define H_GLY_TYPE_RENDER
#ifndef GLY_TYPE_INT
#define GLY_TYPE_INT unsigned char
#endif
#ifdef DOXYGEN
#define GLY_TYPE_SAFE
#endif
void
const char *t,
signed int len,
#endif
const void *const f) {
const void (*const draw_line)(
static const unsigned char number_segments[] = { 0xbf, 0x07, 0x5b, 0x4f,
0x66, 0xec, 0xfc, 0x87,
0xff, 0xef };
static const unsigned char alpha_segments[] = {
0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x7d, 0x76, 0x89,
0x1e, 0x30, 0x38, 0xb7, 0x37, 0x3f, 0x73, 0x67, 0xf3,
0x6d, 0x81, 0x3e, 0xa2, 0xbe, 0x80, 0xe2, 0x88
};
unsigned char c;
GLY_TYPE_INT sp2, sm1, x1, x2, x3, y1, y2, y3;
sp2 = s + 2;
sm1 = s - 1;
x1 = x;
y1 = y;
y2 = y1 + (sm1 / 2);
y3 = y1 + sm1;
while (*t) {
#ifdef GLY_TYPE_SAFE
if (len != -1 && len-- <= 0) {
break;
}
#endif
c = (*t | 0x20) - 'a';
x2 = x1 + (sm1 / 2);
x3 = x1 + sm1;
if ('0' <= *t && *t <= '9') {
unsigned char m = number_segments[*t - '0'];
unsigned char segment = 0;
while (segment < 8) {
switch (m & (1 << segment) ? segment : 8) {
case 0: draw_line(x2, y1, x3, y1); break;
case 1: draw_line(x3, y1, x3, y2); break;
case 2: draw_line(x3, y2, x3, y3); break;
case 3: draw_line(x1, y3, x3, y3); break;
case 4: draw_line(x1, y2, x1, y3); break;
case 5: draw_line(x1, y1, x1, y2); break;
case 6: draw_line(x1, y2, x3, y2); break;
case 7: draw_line(x1, y1, x2, y1); break;
}
segment++;
}
}
if (c == ('k' - 'a')) {
draw_line(x2, y2, x3, y1);
draw_line(x2, y2, x3, y3);
}
if (c <= ('z' - 'a')) {
unsigned char m = alpha_segments[c];
unsigned char segment = 0;
while (segment < 8) {
switch (m & (1 << segment) ? segment : 8) {
case 0: draw_line(x1, y1, x3, y1); break;
case 1: draw_line(x3, y1, x3, y2); break;
case 2: draw_line(x3, y2, x3, y3); break;
case 3: draw_line(x1, y3, x3, y3); break;
case 4: draw_line(x1, y2, x1, y3); break;
case 5: draw_line(x1, y1, x1, y2); break;
case 6: draw_line(x1, y2, x3, y2); break;
case 7: {
if (m & (1 << 6)) {
draw_line(x2, y2, x2, y3);
} else if (m & 1 || m & (1 << 4)) {
draw_line(x2, y1, x2, y3);
} else {
switch ((c + 'a' - 'v' + 1) >> 1) {
case 0:
draw_line(x1, y2, x2, y3);
draw_line(x2, y3, x3, y2);
break;
case 1:
draw_line(x1, y1, x3, y3);
draw_line(x1, y3, x3, y1);
break;
case 2:
draw_line(x1, y1, x3, y1);
draw_line(x1, y3, x3, y1);
break;
}
}
}
case 8: break;
}
segment++;
}
}
x1 += sp2;
t++;
}
}
#endif
#define GLY_TYPE_INT
Definition gly_type_render.h:192
void gly_type_render(GLY_TYPE_INT x, GLY_TYPE_INT y, GLY_TYPE_INT s, const char *t, signed int len, const void *const f)
Definition gly_type_render.h:245
#define GLY_TYPE_SAFE
Definition gly_type_render.h:203

Macro Definition Documentation

◆ GLY_TYPE_INT

#define GLY_TYPE_INT   unsigned char

The GLY_TYPE_INT defines the integer type used for coordinates and sizes in gly_type_render.

  • int but size varies by compiler/architecture. Common in rendering libraries.
  • uint8_t suitable for small screens or limited coordinate ranges.
  • uint16_t often ideal for most cases, balancing range and memory efficiency.
#define GLY_TYPE_INT uint16_t // Recommended for most applications

◆ GLY_TYPE_SAFE

#define GLY_TYPE_SAFE

The GLY_TYPE_SAFE macro enables a length parameter to ensure safe string handling in the gly_type_render function. When defined, it activates an additional check to limit the number of processed characters, preventing access beyond the string's end.

Function Documentation

◆ gly_type_render()

void gly_type_render ( GLY_TYPE_INT x,
GLY_TYPE_INT y,
GLY_TYPE_INT s,
const char * t,
signed int len,
const void *const f )

gly_type_render

The gly_type_render function is used to render text at specified coordinates using a line-drawing function. It employs segments to display characters and allows control over the position and size of the rendered characters.

Precondition
If the optional len parameter is used, GLY_TYPE_SAFE must be defined with #define GLY_TYPE_SAFE prior to including this function.
Parameters
[in]xHorizontal coordinate (in pixels).
[in]yVertical coordinate (in pixels).
[in]sizeCharacter size in pixels.
[in]textText string to be rendered, terminated with a null character (\0).
[in]fptrPointer to a line-drawing function, with the interface: function(x1, y1, x2, y2)
draws a line from (x1, y1) to (x2, y2).

Optional Parameter:

Parameters
[in]lenMaximum length of characters to process, applicable only if GLY_TYPE_SAFE is defined. If len is -1, all characters in the string are processed until the null terminator.

Total Size:

  • Width: length(text) * (size + 1)
  • Height: min(count(text, 0x0A), 1) * (size + 1)

Example Usage:

gly_type_render(x, y, size, "hello world", draw_line_func);

This function will render the text "hello world" at the coordinates (x, y), with the specified character size, using draw_line_func to draw the lines.