Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::gui::Font Class Reference

A class that can be used to draw text. More...

#include <font.h>

Public Member Functions

 Font (const uint8_t *font_data)
 Construct a new Font object.
 
virtual void draw_text (int32_t x, int32_t y, common::Colour foreground_colour, common::Colour background_colour, common::GraphicsContext *context, string text)
 write the entire text to the screen
 
virtual void draw_text (int32_t x, int32_t y, common::Colour foreground_colour, common::Colour background_colour, common::GraphicsContext *context, string text, common::Rectangle< int32_t > limit_area)
 write the entire text to the screen
 
virtual size_t get_text_height (string)
 Get the height of the text.
 
virtual size_t get_text_width (string)
 Get the width of the text.
 

Public Attributes

bool is_bold { false }
 Should the font be drawn in bold.
 
bool is_italic { false }
 Should the font be drawn in italic.
 
bool is_underlined { false }
 Should the font be drawn with an underline.
 
bool is_strikethrough { false }
 Should the font be drawn with a strikethrough.
 

Protected Attributes

bool m_is_8_by_8 = { true }
 Is the font 8 pixels by 8 pixels per character.
 
uint8_t m_font8x8 [2048] = { 0 }
 The 8x8 font data.
 

Detailed Description

A class that can be used to draw text.

Definition at line 27 of file font.h.

Constructor & Destructor Documentation

◆ Font()

Font::Font ( const uint8_t font_data)
explicit

Construct a new Font object.

Parameters
font_dataThe 8x8 font data to use

Definition at line 22 of file font.cpp.

22 {
23
24 // Store the font data
25 for(int i = 0; i < 2048; ++i) {
26 m_font8x8[i] = font_data[i];
27 }
28}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
uint8_t m_font8x8[2048]
The 8x8 font data.
Definition font.h:30

References m_font8x8.

Member Function Documentation

◆ draw_text() [1/2]

void Font::draw_text ( int32_t  x,
int32_t  y,
common::Colour  foreground_colour,
common::Colour  background_colour,
common::GraphicsContext context,
string  text 
)
virtual

write the entire text to the screen

Parameters
xThe x coordinate of the text
yThe y coordinate of the text
foreground_colourThe letter colour
background_colourThe background colour
contextThe graphics context to draw the text on
textThe text to draw

Definition at line 41 of file font.cpp.

43 {
44
45 // Calculate the rectangle of the text
46 int32_t top = 0;
47 int32_t left = 0;
48 int32_t width = get_text_width(text);
49 int32_t height = get_text_height(text);
50
51 // Create the rectangle
52 Rectangle<int32_t> text_area(left, top, width, height);
53
54 // Draw the text
55 draw_text(x, y, foreground_colour, background_colour, context, text, text_area);
56}
virtual size_t get_text_width(string)
Get the width of the text.
Definition font.cpp:152
virtual void draw_text(int32_t x, int32_t y, common::Colour foreground_colour, common::Colour background_colour, common::GraphicsContext *context, string text)
write the entire text to the screen
Definition font.cpp:41
virtual size_t get_text_height(string)
Get the height of the text.
Definition font.cpp:140

References draw_text(), get_text_height(), and get_text_width().

Referenced by MaxOS::gui::widgets::Button::draw(), MaxOS::gui::widgets::InputBox::draw(), MaxOS::gui::widgets::Text::draw(), draw_text(), and MaxOS::drivers::console::VESABootConsole::put_character().

◆ draw_text() [2/2]

void Font::draw_text ( int32_t  x,
int32_t  y,
common::Colour  foreground_colour,
common::Colour  background_colour,
common::GraphicsContext context,
string  text,
common::Rectangle< int32_t limit_area 
)
virtual

write the entire text to the screen

Parameters
xThe x coordinate of the text
yThe y coordinate of the text
foreground_colourThe letter colour
background_colourThe background colour
contextThe graphics context to draw the text on
textThe text to draw
limit_areaThe area of the text to draw

Definition at line 70 of file font.cpp.

73 {
74
75 // Convert the colours
76 uint32_t foreground = context->colour_to_int(foreground_colour);
77 uint32_t background = context->colour_to_int(background_colour);
78
79 // Ensure the area is within the actual area of the text
80 if(limit_area.top < 0) {
82 limit_area.top = 0;
83 }
84
85 if(limit_area.left < 0) {
87 limit_area.left = 0;
88 }
89
90 // Clamp the height and width max
93
96
97 // Calculate limits
100
101 // Draw the text from top to bottom
104
105 // If the y is the middle then add a strikethrough
107
108 // Draw the pixel
110 continue;
111 }
112
113 // If the y is the bottom then add an underline
115
116 // Draw the pixel
118 continue;
119 }
120
121 // Get the character
123
124 // Check if this pixel is set or not
125 bool set = m_font8x8[(uint16_t) character * 8 + y_bit_map_offset] & (128 >> (x_bit_map_offset % 8));
126
127 // Draw the pixel
129
130 }
131 }
132}
Type height
The height of the rectangle.
Definition rectangle.h:27
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Type width
The width of the rectangle.
Definition rectangle.h:26
bool is_underlined
Should the font be drawn with an underline.
Definition font.h:39
bool is_strikethrough
Should the font be drawn with a strikethrough.
Definition font.h:40

References get_text_height(), get_text_width(), MaxOS::common::Rectangle< Type >::height, is_strikethrough, is_underlined, MaxOS::common::Rectangle< Type >::left, m_font8x8, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ get_text_height()

size_t Font::get_text_height ( string  text)
virtual

Get the height of the text.

Parameters
textThe text to get the height of
Returns
The height of the text

Definition at line 140 of file font.cpp.

140 {
141
142 return 8;
143
144}

Referenced by draw_text(), and draw_text().

◆ get_text_width()

size_t Font::get_text_width ( string  text)
virtual

Get the width of the text.

Parameters
textThe text to get the width of
Returns
The width of the text

Definition at line 152 of file font.cpp.

152 {
153
154 return text.length() * 8;
155}

Referenced by draw_text(), and draw_text().

Member Data Documentation

◆ is_bold

bool MaxOS::gui::Font::is_bold { false }

Should the font be drawn in bold.

Definition at line 37 of file font.h.

37{ false };

◆ is_italic

bool MaxOS::gui::Font::is_italic { false }

Should the font be drawn in italic.

Definition at line 38 of file font.h.

38{ false };

◆ is_strikethrough

bool MaxOS::gui::Font::is_strikethrough { false }

Should the font be drawn with a strikethrough.

Definition at line 40 of file font.h.

40{ false };

Referenced by draw_text().

◆ is_underlined

bool MaxOS::gui::Font::is_underlined { false }

Should the font be drawn with an underline.

Definition at line 39 of file font.h.

39{ false };

Referenced by draw_text().

◆ m_font8x8

uint8_t MaxOS::gui::Font::m_font8x8[2048] = { 0 }
protected

The 8x8 font data.

Definition at line 30 of file font.h.

30{ 0 };

Referenced by draw_text(), and Font().

◆ m_is_8_by_8

bool MaxOS::gui::Font::m_is_8_by_8 = { true }
protected

Is the font 8 pixels by 8 pixels per character.

Definition at line 29 of file font.h.

29{ true };

The documentation for this class was generated from the following files: