Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text refactor #470

Merged
merged 9 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ Embedded Graphics is a `no_std` library for adding graphics features to display
- [#386](https://github.com/embedded-graphics/embedded-graphics/pull/386) Added the `delta` method to `Line` to compute the difference between start and end points.
- [#450](https://github.com/embedded-graphics/embedded-graphics/pull/450) Added `ColorConverted` and `DrawTargetExt::color_converted` to support color conversion for draw targets.
- [#438](https://github.com/embedded-graphics/embedded-graphics/pull/438) Added majority CSS web colors as associated `const`s to the RGB color types.
- [#470](https://github.com/embedded-graphics/embedded-graphics/pull/470) Added support for external text renderers. External text renderers can be implemented using the new `TextStyle` trait.

### Changed

- **(breaking)** [#466](https://github.com/embedded-graphics/embedded-graphics/pull/466) Upgrade Nalgebra from 0.19.0 to 0.23.0.
- **(breaking)** [#470](https://github.com/embedded-graphics/embedded-graphics/pull/470) Renamed `Font`, `TextStyle` and `TextStyleBuilder` to `MonoFont`, `MonoTextStyle` and `MonoTextStyleBuilder`.
jamwaffles marked this conversation as resolved.
Show resolved Hide resolved

### Removed

- **(breaking)** [#470](https://github.com/embedded-graphics/embedded-graphics/pull/470) Support for fonts with variable character width was removed from the internal text renderer.
- **(breaking)** [#470](https://github.com/embedded-graphics/embedded-graphics/pull/470) `Font6x6` was removed.

## [0.7.0-alpha.1] - 2020-09-19

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::{PrimitiveStyle, TextStyle},
style::{PrimitiveStyle, MonoTextStyle},
mock_display::MockDisplay,
};

Expand All @@ -161,7 +161,7 @@ fn main() -> Result<(), std::convert::Infallible> {
let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
let fill = PrimitiveStyle::with_fill(BinaryColor::On);
let text_style = TextStyle::new(Font6x8, BinaryColor::On);
let text_style = MonoTextStyle::new(Font6x8, BinaryColor::On);

let yoffset = 10;

Expand Down Expand Up @@ -219,7 +219,7 @@ use embedded_graphics::{
pixelcolor::Rgb565,
prelude::*,
primitives::{Circle, Rectangle},
style::{PrimitiveStyle, TextStyle},
style::{PrimitiveStyle, MonoTextStyle},
};

fn build_thing(text: &'static str) -> impl Iterator<Item = Pixel<Rgb565>> {
Expand All @@ -233,7 +233,7 @@ fn build_thing(text: &'static str) -> impl Iterator<Item = Pixel<Rgb565>> {
)
.chain(
Text::new(text, Point::new(20, 16))
.into_styled(TextStyle::new(Font6x8, Rgb565::GREEN))
.into_styled(MonoTextStyle::new(Font6x8, Rgb565::GREEN))
.into_pixels(),
)
}
Expand Down
6 changes: 3 additions & 3 deletions benches/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use embedded_graphics::{
geometry::Point,
pixelcolor::Gray8,
prelude::*,
style::TextStyle,
style::MonoTextStyle,
};

mod common;
Expand All @@ -14,7 +14,7 @@ use common::Framebuffer;
fn font_6x8(c: &mut Criterion) {
c.bench_function("font 6x8 Hello world!", |b| {
let object = Text::new("Hello world!", Point::zero())
.into_styled(TextStyle::new(Font6x8, Gray8::new(10)));
.into_styled(MonoTextStyle::new(Font6x8, Gray8::new(10)));

let mut framebuffer = Framebuffer::new();
b.iter(|| object.draw(&mut framebuffer))
Expand All @@ -24,7 +24,7 @@ fn font_6x8(c: &mut Criterion) {
fn font_12x16(c: &mut Criterion) {
c.bench_function("font 12x16 Hello world!", |b| {
let object = Text::new("Hello world!", Point::zero())
.into_styled(TextStyle::new(Font12x16, Gray8::new(10)));
.into_styled(MonoTextStyle::new(Font12x16, Gray8::new(10)));

let mut framebuffer = Framebuffer::new();
b.iter(|| object.draw(&mut framebuffer))
Expand Down
Binary file removed data/font6x6.png
Binary file not shown.
Binary file removed data/font6x6_1bpp.raw
Binary file not shown.
16 changes: 8 additions & 8 deletions src/draw_target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,21 +450,21 @@ pub trait DrawTargetExt: DrawTarget + Sized {
/// mock_display::MockDisplay,
/// pixelcolor::BinaryColor,
/// fonts::{Text, Font6x8},
/// style::TextStyle,
/// style::MonoTextStyle,
/// };
///
/// let mut display = MockDisplay::new();
/// let mut translated_display = display.translated(Point::new(10, 5));
///
/// // Draws text at position (10, 5) in the display coordinate system
/// Text::new("Text", Point::zero())
/// .into_styled(TextStyle::new(Font6x8, BinaryColor::On))
/// .into_styled(MonoTextStyle::new(Font6x8, BinaryColor::On))
/// .draw(&mut translated_display)?;
/// #
/// # let mut expected = MockDisplay::new();
/// #
/// # Text::new("Text", Point::new(10, 5))
/// # .into_styled(TextStyle::new(Font6x8, BinaryColor::On))
/// # .into_styled(MonoTextStyle::new(Font6x8, BinaryColor::On))
/// # .draw(&mut expected)?;
/// #
/// # assert_eq!(display, expected);
Expand Down Expand Up @@ -493,7 +493,7 @@ pub trait DrawTargetExt: DrawTarget + Sized {
/// mock_display::MockDisplay,
/// pixelcolor::Rgb565,
/// fonts::{Text, Font6x8},
/// style::TextStyle,
/// style::MonoTextStyle,
/// primitives::Rectangle,
/// };
///
Expand All @@ -510,7 +510,7 @@ pub trait DrawTargetExt: DrawTarget + Sized {
/// let text_position = Point::zero() + (target_size - text_size) / 2;
///
/// Text::new(text, text_position)
/// .into_styled(TextStyle::new(Font6x8, Rgb565::YELLOW))
/// .into_styled(MonoTextStyle::new(Font6x8, Rgb565::YELLOW))
/// .draw(target)
/// }
///
Expand Down Expand Up @@ -545,7 +545,7 @@ pub trait DrawTargetExt: DrawTarget + Sized {
/// mock_display::MockDisplay,
/// pixelcolor::BinaryColor,
/// fonts::{Text, Font12x16},
/// style::TextStyle,
/// style::MonoTextStyle,
/// primitives::Rectangle,
/// };
///
Expand All @@ -557,13 +557,13 @@ pub trait DrawTargetExt: DrawTarget + Sized {
/// // Only the first 4 characters will be drawn, because the others are outside
/// // the clipping area
/// Text::new("Clipped", Point::zero())
/// .into_styled(TextStyle::new(Font12x16, BinaryColor::On))
/// .into_styled(MonoTextStyle::new(Font12x16, BinaryColor::On))
/// .draw(&mut clipped_display)?;
/// #
/// # let mut expected = MockDisplay::new();
/// #
/// # Text::new("Clip", Point::zero())
/// # .into_styled(TextStyle::new(Font12x16, BinaryColor::On))
/// # .into_styled(MonoTextStyle::new(Font12x16, BinaryColor::On))
/// # .draw(&mut expected)?;
/// #
/// # assert_eq!(display, expected);
Expand Down
4 changes: 2 additions & 2 deletions src/drawable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{draw_target::DrawTarget, geometry::Point, pixelcolor::PixelColor};
/// pixelcolor::{BinaryColor, PixelColor, Rgb888},
/// prelude::*,
/// primitives::Rectangle,
/// style::{PrimitiveStyle, TextStyle},
/// style::{PrimitiveStyle, MonoTextStyle},
/// };
///
/// struct Button<'a, C: PixelColor> {
Expand All @@ -39,7 +39,7 @@ use crate::{draw_target::DrawTarget, geometry::Point, pixelcolor::PixelColor};
/// .draw(display)?;
///
/// Text::new(self.text, Point::new(6, 6))
/// .into_styled(TextStyle::new(Font6x8, self.fg_color))
/// .into_styled(MonoTextStyle::new(Font6x8, self.fg_color))
/// .draw(display)
/// }
/// }
Expand Down
10 changes: 5 additions & 5 deletions src/fonts/font12x16.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{fonts::Font, geometry::Size};
use crate::{fonts::MonoFont, geometry::Size};

/// 12x16 pixel monospace font.
///
Expand All @@ -11,7 +11,7 @@ use crate::{fonts::Font, geometry::Size};
pub struct Font12x16;

/// Config for 12x16 font
impl Font for Font12x16 {
impl MonoFont for Font12x16 {
const FONT_IMAGE: &'static [u8] = include_bytes!("../../data/font12x16_1bpp.raw");
const FONT_IMAGE_WIDTH: u32 = 480;

Expand All @@ -36,10 +36,10 @@ impl Font for Font12x16 {
mod tests {
use super::*;
use crate::{
fonts::{tests::assert_text_from_pattern, Font, Text},
fonts::{tests::assert_text_from_pattern, MonoFont, Text},
geometry::{Dimensions, Point, Size},
pixelcolor::BinaryColor,
style::TextStyle,
style::MonoTextStyle,
};

const WIDTH: usize = Font12x16::CHARACTER_SIZE.width as usize;
Expand All @@ -48,7 +48,7 @@ mod tests {

#[test]
fn text_dimensions() {
let style = TextStyle::new(Font12x16, BinaryColor::On);
let style = MonoTextStyle::new(Font12x16, BinaryColor::On);
let hello = Text::new(HELLO_WORLD, Point::zero()).into_styled(style);
let empty = Text::new("", Point::zero()).into_styled(style);

Expand Down
10 changes: 5 additions & 5 deletions src/fonts/font24x32.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{fonts::Font, geometry::Size};
use crate::{fonts::MonoFont, geometry::Size};

/// 24x32 pixel monospace font.
///
Expand All @@ -16,7 +16,7 @@ use crate::{fonts::Font, geometry::Size};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Font24x32;

impl Font for Font24x32 {
impl MonoFont for Font24x32 {
const FONT_IMAGE: &'static [u8] = include_bytes!("../../data/font24x32_1bpp.raw");
const FONT_IMAGE_WIDTH: u32 = 960;

Expand All @@ -41,10 +41,10 @@ impl Font for Font24x32 {
mod tests {
use super::*;
use crate::{
fonts::{tests::assert_text_from_pattern, Font, Text},
fonts::{tests::assert_text_from_pattern, MonoFont, Text},
geometry::{Dimensions, Point, Size},
pixelcolor::BinaryColor,
style::TextStyle,
style::MonoTextStyle,
};

const WIDTH: usize = Font24x32::CHARACTER_SIZE.width as usize;
Expand All @@ -53,7 +53,7 @@ mod tests {

#[test]
fn text_dimensions() {
let style = TextStyle::new(Font24x32, BinaryColor::On);
let style = MonoTextStyle::new(Font24x32, BinaryColor::On);
let hello = Text::new(HELLO_WORLD, Point::zero()).into_styled(style);
let empty = Text::new("", Point::zero()).into_styled(style);

Expand Down
10 changes: 5 additions & 5 deletions src/fonts/font6x12.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{fonts::Font, geometry::Size};
use crate::{fonts::MonoFont, geometry::Size};

/// 6x12 pixel monospace font.
///
Expand All @@ -10,7 +10,7 @@ use crate::{fonts::Font, geometry::Size};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Font6x12;

impl Font for Font6x12 {
impl MonoFont for Font6x12 {
const FONT_IMAGE: &'static [u8] = include_bytes!("../../data/font6x12_1bpp.raw");
const FONT_IMAGE_WIDTH: u32 = 96;

Expand All @@ -32,10 +32,10 @@ impl Font for Font6x12 {
mod tests {
use super::*;
use crate::{
fonts::{tests::assert_text_from_pattern, Font, Text},
fonts::{tests::assert_text_from_pattern, MonoFont, Text},
geometry::{Dimensions, Point, Size},
pixelcolor::BinaryColor,
style::TextStyle,
style::MonoTextStyle,
};

const WIDTH: usize = Font6x12::CHARACTER_SIZE.width as usize;
Expand All @@ -44,7 +44,7 @@ mod tests {

#[test]
fn text_dimensions() {
let style = TextStyle::new(Font6x12, BinaryColor::On);
let style = MonoTextStyle::new(Font6x12, BinaryColor::On);
let hello = Text::new(HELLO_WORLD, Point::zero()).into_styled(style);
let empty = Text::new("", Point::zero()).into_styled(style);

Expand Down
Loading