1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
//! This crate contains functions to print strings in a framebuffer.
//! The coordinate in these functions is relative to the origin(top-left point) of the framebuffer.
#![no_std]
extern crate alloc;
extern crate font;
extern crate framebuffer;
extern crate shapes;
use alloc::vec;
use font::{CHARACTER_HEIGHT, CHARACTER_WIDTH};
use framebuffer::{Framebuffer, Pixel};
use shapes::{Coord, Rectangle};
type Ascii = u8;
/// Prints a string in a framebuffer.
/// Returns (column, line, rectangle), i.e. the position of the next symbol and an rectangle which covers the updated area.
/// A block item (index, width) represents the index of line number and the width of charaters in this line as pixels. It can be viewed as a framebuffer block which is described in the `framebuffer_compositor` crate.
/// # Arguments
/// * `framebuffer`: the framebuffer to display in.
/// * `coordinate`: the left top coordinate of the text block relative to the origin(top-left point) of the framebuffer.
/// * `width`, `height`: the size of the text block in number of pixels.
/// * `slice`: the string to display.
/// * `fg_pixel`: the value of pixels in the foreground.
/// * `bg_pixel` the value of pixels in the background.
/// * `column`, `line`: the location of the text in the text block in number of characters.
#[allow(clippy::too_many_arguments)]
pub fn print_string<P: Pixel>(
framebuffer: &mut Framebuffer<P>,
coordinate: Coord,
width: usize,
height: usize,
slice: &str,
fg_pixel: P,
bg_pixel: P,
column: usize,
line: usize,
) -> (usize, usize, Rectangle) {
let buffer_width = width / CHARACTER_WIDTH;
let buffer_height = height / CHARACTER_HEIGHT;
let (x, y) = (coordinate.x, coordinate.y);
let mut curr_line = line;
let mut curr_column = column;
let top_left = Coord::new(0, (curr_line * CHARACTER_HEIGHT) as isize);
for byte in slice.bytes() {
if byte == b'\n' {
let mut blank = Rectangle {
top_left: Coord::new(
coordinate.x + (curr_column * CHARACTER_WIDTH) as isize,
coordinate.y + (curr_line * CHARACTER_HEIGHT) as isize,
),
bottom_right: Coord::new(
coordinate.x + width as isize,
coordinate.y + ((curr_line + 1) * CHARACTER_HEIGHT) as isize,
)
};
// fill the remaining blank of current line and go to the next line
fill_blank(
framebuffer,
&mut blank,
bg_pixel,
);
curr_column = 0;
curr_line += 1;
if curr_line == buffer_height {
break;
}
} else {
if curr_column == buffer_width {
curr_column = 0;
curr_line += 1;
if curr_line == buffer_height {
break;
}
}
// print the next character
print_ascii_character(
framebuffer,
byte,
fg_pixel,
bg_pixel,
coordinate,
curr_column,
curr_line,
);
curr_column += 1;
}
}
let mut blank = Rectangle {
top_left: Coord::new(
x + (curr_column * CHARACTER_WIDTH) as isize,
y + (curr_line * CHARACTER_HEIGHT) as isize,
),
bottom_right: Coord::new(
x + width as isize,
y + ((curr_line + 1) * CHARACTER_HEIGHT) as isize,
)
};
// fill the blank of the last line
fill_blank(
framebuffer,
&mut blank,
bg_pixel,
);
let bottom_right = Coord::new(
(buffer_width * CHARACTER_WIDTH) as isize,
((curr_line + 1) * CHARACTER_HEIGHT) as isize
);
let update_area = Rectangle {
top_left,
bottom_right,
};
// fill the blank of the remaining part
blank = Rectangle {
top_left: Coord::new(
x,
y + ((curr_line + 1) * CHARACTER_HEIGHT) as isize,
),
bottom_right: Coord::new(
x + width as isize,
y + height as isize,
)
};
fill_blank(
framebuffer,
&mut blank,
bg_pixel,
);
// return the position of next symbol and updated blocks.
(curr_column, curr_line, update_area)
}
/// Prints a character to the framebuffer at position (line, column) of all characters in the text area.
/// # Arguments
/// * `framebuffer`: the framebuffer to display in.
/// * `character`: the ASCII code of the character to display.
/// * `fg_pixel`: the value of every pixel in the character.
/// * `bg_color`: the value of every pixel in the background.
/// * `coordinate`: the left top coordinate of the text block relative to the origin(top-left point) of the framebuffer.
/// * `column`, `line`: the location of the character in the text block as symbols.
pub fn print_ascii_character<P: Pixel>(
framebuffer: &mut Framebuffer<P>,
character: Ascii,
fg_pixel: P,
bg_pixel: P,
coordinate: Coord,
column: usize,
line: usize,
) {
let start = coordinate + ((column * CHARACTER_WIDTH) as isize, (line * CHARACTER_HEIGHT) as isize);
if !framebuffer.overlaps_with(start, CHARACTER_WIDTH, CHARACTER_HEIGHT) {
return
}
// print from the offset within the framebuffer
let (buffer_width, buffer_height) = framebuffer.get_size();
let off_set_x: usize = if start.x < 0 { -(start.x) as usize } else { 0 };
let off_set_y: usize = if start.y < 0 { -(start.y) as usize } else { 0 };
let mut j = off_set_x;
let mut i = off_set_y;
loop {
let coordinate = start + (j as isize, i as isize);
if framebuffer.contains(coordinate) {
let pixel = if j >= 1 {
// leave 1 pixel gap between two characters
let index = j - 1;
let char_font = font::FONT_BASIC[character as usize][i];
if get_bit(char_font, index) != 0 {
fg_pixel
} else {
bg_pixel
}
} else {
bg_pixel
};
framebuffer.draw_pixel(coordinate, pixel);
}
j += 1;
if j == CHARACTER_WIDTH || start.x + j as isize == buffer_width as isize {
i += 1;
if i == CHARACTER_HEIGHT || start.y + i as isize == buffer_height as isize {
return
}
j = off_set_x;
}
}
}
/// Fill a blank text area (left, top, right, bottom) with color. The tuple specifies the location of the area relative to the origin(top-left point) of the framebuffer.
pub fn fill_blank<P: Pixel>(
framebuffer: &mut Framebuffer<P>,
blank: &mut Rectangle,
pixel: P,
) {
let (width, height) = framebuffer.get_size();
// fill the part within the framebuffer
blank.top_left.x = core::cmp::max(0, blank.top_left.x);
blank.top_left.y = core::cmp::max(0, blank.top_left.y);
blank.bottom_right.x = core::cmp::min(blank.bottom_right.x, width as isize);
blank.bottom_right.y = core::cmp::min(blank.bottom_right.y, height as isize);
if blank.top_left.x >= blank.bottom_right.x ||
blank.top_left.y >= blank.bottom_right.y {
return
}
let fill = vec![pixel; (blank.bottom_right.x - blank.top_left.x) as usize];
let mut coordinate = blank.top_left;
loop {
if coordinate.y == blank.bottom_right.y {
return
}
if let Some(start) = framebuffer.index_of(coordinate) {
framebuffer.composite_buffer(&fill, start);
}
coordinate.y += 1;
}
}
/// Gets the i_th most significant bit of `char_font`. The returned value is `1` or `0`.
fn get_bit(char_font: u8, i: usize) -> u8 {
char_font & (0x80 >> i)
}