Max OS 0.3
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1
11#include <gui/font.h>
12
13using namespace MaxOS;
14using namespace MaxOS::common;
15using namespace MaxOS::gui;
16
23
24 // Store the font data
25 for(int i = 0; i < 2048; ++i) {
26 m_font8x8[i] = font_data[i];
27 }
28}
29
30Font::~Font() = default;
31
41void Font::draw_text(int32_t x, int32_t y, common::Colour foreground_colour,
42 common::Colour background_colour,
43 common::GraphicsContext* context, string text) {
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}
57
58
70void Font::draw_text(int32_t x, int32_t y, common::Colour foreground_colour,
71 common::Colour background_colour,
72 common::GraphicsContext* context, string text,
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}
133
140size_t Font::get_text_height(string text) {
141
142 return 8;
143
144}
145
152size_t Font::get_text_width(string text) {
153
154 return text.length() * 8;
155}
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
Draws pixels to the screen, and handles drawing lines, rectangles and circles.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
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
uint8_t m_font8x8[2048]
The 8x8 font data.
Definition font.h:30
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
Font(const uint8_t *font_data)
Construct a new Font object.
Definition font.cpp:22
Defines a Font class for drawing text onto a graphics context.