forked from AGL/AGL_Simple_Impl
Merge branch 'master' of ssh://git.ablecorp.us:20/AGL/AGL_Simple_Impl
This commit is contained in:
commit
f8729fea9b
|
@ -1,6 +1,6 @@
|
||||||
#![allow(dead_code, unused)]
|
#![allow(dead_code, unused)]
|
||||||
|
|
||||||
use ab_glyph::FontRef;
|
use ab_glyph::{FontRef, ScaleFont};
|
||||||
use able_graphics_library::{AglApi, FrameBuffer, Point, HEIGHT, RGBA, WIDTH};
|
use able_graphics_library::{AglApi, FrameBuffer, Point, HEIGHT, RGBA, WIDTH};
|
||||||
use mini_gl_fb::glutin::platform::unix::x11::ffi::XK_R10;
|
use mini_gl_fb::glutin::platform::unix::x11::ffi::XK_R10;
|
||||||
|
|
||||||
|
@ -80,26 +80,51 @@ impl AglApi for GraphicsRenderer {
|
||||||
fn draw_text(&mut self, coords: Point, scale: u8, color: RGBA, text: String) {
|
fn draw_text(&mut self, coords: Point, scale: u8, color: RGBA, text: String) {
|
||||||
use ab_glyph::{point, Font, FontRef, Glyph};
|
use ab_glyph::{point, Font, FontRef, Glyph};
|
||||||
|
|
||||||
let font: FontRef =
|
let font = FontRef::try_from_slice(include_bytes!("../font/Roboto-Regular.ttf")).unwrap();
|
||||||
FontRef::try_from_slice(include_bytes!("../font/Roboto-Regular.ttf")).unwrap();
|
let font_scaled = font.as_scaled(scale as f32);
|
||||||
|
|
||||||
// Get a glyph for 'q' with a scale & position.
|
// Individual characters are drawn at floating-point offsets.
|
||||||
let q_glyph: Glyph = font
|
let mut coords = point(coords.x as f32, coords.y as f32);
|
||||||
.glyph_id('a')
|
let mut last_char = None;
|
||||||
|
|
||||||
|
for ch in text.chars() {
|
||||||
|
let glyph_id = font.glyph_id(ch);
|
||||||
|
|
||||||
|
// Kern character pairs as necessary.
|
||||||
|
if let Some(last) = last_char {
|
||||||
|
coords.x -= font_scaled.kern(last, glyph_id);
|
||||||
|
}
|
||||||
|
last_char = Some(font.glyph_id(ch));
|
||||||
|
|
||||||
|
// Get a glyph with a scale & position.
|
||||||
|
let glyph: Glyph = glyph_id
|
||||||
.with_scale_and_position(scale as f32, point(coords.x as f32, coords.y as f32));
|
.with_scale_and_position(scale as f32, point(coords.x as f32, coords.y as f32));
|
||||||
|
|
||||||
// Draw it.
|
// Draw it.
|
||||||
if let Some(q) = font.outline_glyph(q_glyph) {
|
if let Some(outline) = font.outline_glyph(glyph) {
|
||||||
// dbg!(q);
|
outline.draw(|x, y, c| {
|
||||||
|
// ab_glyph gives us coordinates within the
|
||||||
|
// bounding box; convert these to coordinates
|
||||||
|
// within the image.
|
||||||
|
let corner = outline.px_bounds().min;
|
||||||
|
let (x, y) = (x + corner.x as u32, y + corner.y as u32);
|
||||||
|
|
||||||
q.draw(|x, y, c| {
|
// Flip y to draw characters right-side-up.
|
||||||
/* draw pixel `(x, y)` with coverage: `c` */
|
let y = 2 * coords.y as u32 - y;
|
||||||
dbg!(x, y, c);
|
|
||||||
|
|
||||||
|
// draw pixel `(x, y)` with coverage: `c`
|
||||||
let coordinates = x as u16 + (WIDTH as u16).wrapping_mul(y as u16);
|
let coordinates = x as u16 + (WIDTH as u16).wrapping_mul(y as u16);
|
||||||
|
let pixel = &mut self.buff[coordinates as usize];
|
||||||
|
|
||||||
self.buff[coordinates as usize] = color;
|
// Interpolate between the existing background
|
||||||
|
// character and the new foreground color.
|
||||||
|
*pixel = lerp_rgba(*pixel, color, c);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Advance to the next glyph's position.
|
||||||
|
coords.x += font_scaled.h_advance(glyph_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// Actually move the double buffer to the single buffer and "update" the screen
|
/// Actually move the double buffer to the single buffer and "update" the screen
|
||||||
fn draw(&mut self) {}
|
fn draw(&mut self) {}
|
||||||
|
@ -109,3 +134,16 @@ impl AglApi for GraphicsRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Linearly interpolate between two colors according to a factor.
|
||||||
|
/// This method is cheap, perfectly accurate for shades of gray, and
|
||||||
|
/// reasonably accurate for other colors; if better results are
|
||||||
|
/// needed, LCH interpolation should be used instead.
|
||||||
|
fn lerp_rgba(a: RGBA, b: RGBA, fac: f32) -> RGBA {
|
||||||
|
RGBA {
|
||||||
|
r: ((b.r as f32 - a.r as f32) * fac + a.r as f32) as _,
|
||||||
|
g: ((b.g as f32 - a.g as f32) * fac + a.g as f32) as _,
|
||||||
|
b: ((b.b as f32 - a.b as f32) * fac + a.b as f32) as _,
|
||||||
|
a: ((b.a as f32 - a.a as f32) * fac + a.a as f32) as _,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -73,8 +73,8 @@ fn main() {
|
||||||
);
|
);
|
||||||
|
|
||||||
xyz.draw_text(
|
xyz.draw_text(
|
||||||
Point { x: 1000, y: 90 },
|
Point { x: 100, y: 90 },
|
||||||
1,
|
24,
|
||||||
RGBA {
|
RGBA {
|
||||||
r: 0,
|
r: 0,
|
||||||
g: 0,
|
g: 0,
|
||||||
|
|
Loading…
Reference in a new issue