Skip to content

Commit 0d9995b

Browse files
committed
fb/logger: bump noto-sans-mono-bitmap + improvements
1 parent 515a7ec commit 0d9995b

File tree

3 files changed

+76
-33
lines changed

3 files changed

+76
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,24 @@ rand = { version = "0.8.4", optional = true, default-features = false }
5656
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
5757

5858
[dependencies.noto-sans-mono-bitmap]
59-
version = "0.1.2"
59+
path = "../noto-sans-mono-bitmap"
6060
default-features = false
61-
features = ["regular", "size_14"]
61+
features = [
62+
"regular",
63+
"size_16",
64+
"unicode-basic-latin",
65+
"unicode-latin-1-supplement",
66+
"unicode-latin-extended-a",
67+
"unicode-specials"
68+
]
6269
optional = true
6370

6471
[build-dependencies]
6572
llvm-tools-build = { version = "0.1", optional = true, package = "llvm-tools" }
6673
toml = { version = "0.5.1", optional = true }
67-
serde = { version = "1.0", features = ["derive"], optional = true}
68-
quote = { version = "1.0", optional = true}
69-
proc-macro2 = { version = "1.0", optional = true}
74+
serde = { version = "1.0", features = ["derive"], optional = true }
75+
quote = { version = "1.0", optional = true }
76+
proc-macro2 = { version = "1.0", optional = true }
7077

7178
[features]
7279
default = []
@@ -94,6 +101,6 @@ default-target = "x86_64-unknown-linux-gnu"
94101
[package.metadata.release]
95102
dev-version = false
96103
pre-release-replacements = [
97-
{ file="Changelog.md", search="# Unreleased", replace="# Unreleased\n\n# {{version}} – {{date}}", exactly=1 },
104+
{ file = "Changelog.md", search = "# Unreleased", replace = "# Unreleased\n\n# {{version}} – {{date}}", exactly = 1 },
98105
]
99106
pre-release-commit-message = "Release version {{version}}"

src/binary/logger.rs

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use crate::binary::logger::font_constants::BACKUP_CHAR;
12
use crate::boot_info::{FrameBufferInfo, PixelFormat};
23
use conquer_once::spin::OnceCell;
34
use core::{
45
fmt::{self, Write},
56
ptr,
67
};
7-
use noto_sans_mono_bitmap::{get_bitmap, get_bitmap_width, BitmapChar, BitmapHeight, FontWeight};
8+
use noto_sans_mono_bitmap::{
9+
get_raster, get_raster_width, FontWeight, RasterHeight, RasterizedChar,
10+
};
811
use spinning_top::Spinlock;
912

1013
/// The global logger instance used for the `log` crate.
@@ -14,9 +17,46 @@ pub static LOGGER: OnceCell<LockedLogger> = OnceCell::uninit();
1417
pub struct LockedLogger(Spinlock<Logger>);
1518

1619
/// Additional vertical space between lines
17-
const LINE_SPACING: usize = 0;
18-
/// Additional vertical space between separate log messages
19-
const LOG_SPACING: usize = 2;
20+
const LINE_SPACING: usize = 2;
21+
/// Additional horizontal space between characters.
22+
const LETTER_SPACING: usize = 0;
23+
24+
/// Padding from the border. Prevent that font is too close to border.
25+
const BORDER_PADDING: usize = 1;
26+
27+
/// Constants for the usage of the [`noto_sans_mono_bitmap`] crate.
28+
mod font_constants {
29+
use super::*;
30+
31+
/// Height of each char raster. The font size is ~0.84% of this. Thus, this is the line height that
32+
/// enables multiple characters to be side-by-side and appear optically in one line in a natural way.
33+
pub const CHAR_RASTER_HEIGHT: RasterHeight = RasterHeight::Size16;
34+
35+
/// The width of each single symbol of the mono space font.
36+
pub const CHAR_RASTER_WIDTH: usize = get_raster_width(FontWeight::Regular, CHAR_RASTER_HEIGHT);
37+
38+
/// Requires feature "unicode-specials"
39+
pub const BACKUP_CHAR: char = '�';
40+
41+
pub const FONT_WEIGHT: FontWeight = FontWeight::Regular;
42+
}
43+
44+
/// Returns the raster of the given char or the raster of [`font_constants::BACKUP_CHAR`].
45+
fn get_char_raster(c: char) -> RasterizedChar {
46+
get_raster(
47+
c,
48+
font_constants::FONT_WEIGHT,
49+
font_constants::CHAR_RASTER_HEIGHT,
50+
)
51+
.unwrap_or(
52+
get_raster(
53+
BACKUP_CHAR,
54+
font_constants::FONT_WEIGHT,
55+
font_constants::CHAR_RASTER_HEIGHT,
56+
)
57+
.unwrap(),
58+
)
59+
}
2060

2161
impl LockedLogger {
2262
/// Create a new instance that logs to the given framebuffer.
@@ -39,8 +79,7 @@ impl log::Log for LockedLogger {
3979

4080
fn log(&self, record: &log::Record) {
4181
let mut logger = self.0.lock();
42-
writeln!(logger, "{}: {}", record.level(), record.args()).unwrap();
43-
logger.add_vspace(LOG_SPACING);
82+
writeln!(logger, "{:5}: {}", record.level(), record.args()).unwrap();
4483
}
4584

4685
fn flush(&self) {}
@@ -68,22 +107,18 @@ impl Logger {
68107
}
69108

70109
fn newline(&mut self) {
71-
self.y_pos += 14 + LINE_SPACING;
110+
self.y_pos += font_constants::CHAR_RASTER_HEIGHT.val() + LINE_SPACING;
72111
self.carriage_return()
73112
}
74113

75-
fn add_vspace(&mut self, space: usize) {
76-
self.y_pos += space;
77-
}
78-
79114
fn carriage_return(&mut self) {
80-
self.x_pos = 0;
115+
self.x_pos = BORDER_PADDING;
81116
}
82117

83-
/// Erases all text on the screen.
118+
/// Erases all text on the screen. Resets `self.x_pos` and `self.y_pos`.
84119
pub fn clear(&mut self) {
85-
self.x_pos = 0;
86-
self.y_pos = 0;
120+
self.x_pos = BORDER_PADDING;
121+
self.y_pos = BORDER_PADDING;
87122
self.framebuffer.fill(0);
88123
}
89124

@@ -95,32 +130,35 @@ impl Logger {
95130
self.info.vertical_resolution
96131
}
97132

133+
/// Writes a single char to the framebuffer. Takes care of special control characters, such as
134+
/// newlines and carriage returns.
98135
fn write_char(&mut self, c: char) {
99136
match c {
100137
'\n' => self.newline(),
101138
'\r' => self.carriage_return(),
102139
c => {
103-
if self.x_pos >= self.width() {
140+
let new_xpos = self.x_pos + font_constants::CHAR_RASTER_WIDTH;
141+
if new_xpos >= self.width() {
104142
self.newline();
105143
}
106-
const BITMAP_LETTER_WIDTH: usize =
107-
get_bitmap_width(FontWeight::Regular, BitmapHeight::Size14);
108-
if self.y_pos >= (self.height() - BITMAP_LETTER_WIDTH) {
144+
let new_ypos = self.y_pos + font_constants::CHAR_RASTER_HEIGHT.val() + BORDER_PADDING;
145+
if new_ypos >= self.height() {
109146
self.clear();
110147
}
111-
let bitmap_char = get_bitmap(c, FontWeight::Regular, BitmapHeight::Size14).unwrap();
112-
self.write_rendered_char(bitmap_char);
148+
self.write_rendered_char(get_char_raster(c));
113149
}
114150
}
115151
}
116152

117-
fn write_rendered_char(&mut self, rendered_char: BitmapChar) {
118-
for (y, row) in rendered_char.bitmap().iter().enumerate() {
153+
/// Prints a rendered char into the framebuffer.
154+
/// Updates `self.x_pos`.
155+
fn write_rendered_char(&mut self, rendered_char: RasterizedChar) {
156+
for (y, row) in rendered_char.raster().iter().enumerate() {
119157
for (x, byte) in row.iter().enumerate() {
120158
self.write_pixel(self.x_pos + x, self.y_pos + y, *byte);
121159
}
122160
}
123-
self.x_pos += rendered_char.width();
161+
self.x_pos += rendered_char.width() + LETTER_SPACING;
124162
}
125163

126164
fn write_pixel(&mut self, x: usize, y: usize, intensity: u8) {

0 commit comments

Comments
 (0)