embedded_picofont/
lib.rs

1//! *The PICO-8 font to use with [`embedded-graphics`](https://crates.io/crates/embedded-graphics).*
2//!
3//! [![TravisCI](https://img.shields.io/travis/althonos/embedded-picofont/master.svg?maxAge=600&style=flat-square)](https://travis-ci.org/althonos/embedded-picofont/branches)
4//! [![Codecov](https://img.shields.io/codecov/c/gh/althonos/embedded-picofont/master.svg?style=flat-square&maxAge=600)](https://codecov.io/gh/althonos/embedded-picofont)
5//! [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square&maxAge=2678400)](https://choosealicense.com/licenses/mit/)
6//! [![Source](https://img.shields.io/badge/source-GitHub-303030.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/embedded-picofont)
7//! [![Crate](https://img.shields.io/crates/v/embedded-picofont.svg?maxAge=600&style=flat-square)](https://crates.io/crates/embedded-picofont)
8//! [![Documentation](https://img.shields.io/badge/docs.rs-latest-4d76ae.svg?maxAge=2678400&style=flat-square)](https://docs.rs/embedded-picofont)
9//! [![Changelog](https://img.shields.io/badge/keep%20a-changelog-8A0707.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/embedded-picofont/blob/master/CHANGELOG.md)
10//! [![GitHub issues](https://img.shields.io/github/issues/althonos/embedded-picofont.svg?style=flat-square)](https://github.com/althonos/embedded-picofont/issues)
11//!
12//! # Overview
13//!
14//! This crate provides the super tiny 4x6 font of the PICO-8 fantasy console as an [`embedded_graphics::fonts::Font`](https://docs.rs/embedded-graphics/0.6.2/embedded_graphics/fonts/trait.Font.html)::
15//!
16//! <img src="https://www.lexaloffle.com/gfx/pico8_font.png" style="width:100%;image-rendering:-moz-crisp-edges;image-rendering:-o-crisp-edges;image-rendering:-webkit-optimize-contrast;image-rendering: crisp-edges;-ms-interpolation-mode: nearest-neighbor;">
17//!
18//! Please note the PICO-8 itself only use the uppercase characters, as the lowercase
19//! chars can get *really* hard to read if the display is not upscaled. As such, it
20//! is advised to only use this font to display uppercase characters.
21//!
22//! # Usage
23//!
24//! Use [`TextStyle`](https://docs.rs/embedded-graphics/0.6.2/embedded_graphics/style/struct.TextStyle.html)
25//! to attach the PICO-8 font to a text:
26//! ```rust
27//! use embedded_graphics::prelude::*;
28//! use embedded_graphics::fonts::Text;
29//! use embedded_graphics::pixelcolor::Gray8;
30//! use embedded_graphics::style::TextStyle;
31//! use embedded_picofont::FontPico;
32//!
33//! let text = Text::new("Hello world!", Point::new(0, 0))
34//!     .into_styled(TextStyle::new(FontPico, Gray8::WHITE));
35//! ```
36//! ![Hello world](https://github.com/althonos/embedded-picofont/raw/master/static/helloworld.png)
37//!
38//! The PICO-8 also has wide characters: these can be displayed using two smaller
39//! characters in the `128..255` char range:
40//! ```rust
41//! # use embedded_graphics::prelude::*;
42//! # use embedded_graphics::fonts::Text;
43//! # use embedded_graphics::pixelcolor::Gray8;
44//! # use embedded_graphics::style::TextStyle;
45//! # use embedded_picofont::FontPico;
46//!
47//! let text = Text::new("PRESS \u{96}\u{97} TO GO BACK", Point::new(0, 0))
48//!     .into_styled(TextStyle::new(FontPico, Gray8::WHITE));
49//! ```
50//! ![Press left to go back](https://github.com/althonos/embedded-picofont/raw/master/static/goback.png)
51//!
52//! See the [`embedded-graphics` documentation](https://docs.rs/embedded-graphics/)
53//! for more information on how to use this crate.
54//!
55//! # License
56//!
57//! * The [original PICO-8 font](https://www.lexaloffle.com/pico-8.php?page=faq)
58//!   is available under the [CC-0 license](https://creativecommons.org/share-your-work/public-domain/cc0/).
59//! * This crate is released under the [MIT License](https://opensource.org/licenses/mit-license.php).
60//!
61
62#![no_std]
63
64use embedded_graphics::fonts::Font;
65use embedded_graphics::geometry::Size;
66
67/// The 4x6 pixel monospace font used by the PICO-fantasy 8 console.
68///
69/// # Examples
70///
71/// ## Write some text to the screen at the default `(0, 0)` position
72///
73/// ```rust
74/// # use embedded_graphics::prelude::*;
75/// # use embedded_graphics::fonts::Text;
76/// # use embedded_graphics::pixelcolor::Gray8;
77/// # use embedded_graphics::style::TextStyle;
78/// # use embedded_picofont::FontPico;
79/// # use embedded_graphics::mock_display::MockDisplay;
80/// # let mut display = MockDisplay::default();
81/// Text::new("Hello Rust!", Point::new(0, 0))
82///     .into_styled(TextStyle::new(FontPico, Gray8::WHITE))
83///     .draw(&mut display);
84/// ```
85///
86/// ## Translate text by (20px, 30px)
87///
88/// ```rust
89/// # use embedded_graphics::prelude::*;
90/// # use embedded_graphics::fonts::Text;
91/// # use embedded_graphics::pixelcolor::Gray8;
92/// # use embedded_graphics::style::TextStyle;
93/// # use embedded_picofont::FontPico;
94/// # use embedded_graphics::mock_display::MockDisplay;
95/// # let mut display = MockDisplay::default();
96/// Text::new("Hello Rust!", Point::new(20, 30))
97///     .into_styled(TextStyle::new(FontPico, Gray8::WHITE))
98///     .draw(&mut display);
99/// ```
100///
101/// ## Add some styling to the text
102///
103/// Use the [`TextStyleBuilder`](https://docs.rs/embedded-graphics/0.6.1/embedded_graphics/style/struct.TextStyleBuilder.html)
104/// trait to edit the colors of the rendered text:
105///
106/// ```rust
107/// # use embedded_graphics::prelude::*;
108/// # use embedded_graphics::fonts::Text;
109/// # use embedded_graphics::pixelcolor::Rgb888;
110/// # use embedded_graphics::style::TextStyleBuilder;
111/// # use embedded_picofont::FontPico;
112/// # use embedded_graphics::mock_display::MockDisplay;
113/// # let mut display = MockDisplay::default();
114/// let style = TextStyleBuilder::new(FontPico)
115///     .text_color(Rgb888::MAGENTA)
116///     .background_color(Rgb888::BLACK)
117///     .build();
118///
119/// Text::new("Hello Rust!", Point::new(0, 0))
120///     .into_styled(style)
121///     .draw(&mut display);
122/// ```
123#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
124pub struct FontPico;
125
126impl Font for FontPico {
127    const FONT_IMAGE: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/font.raw"));
128    const FONT_IMAGE_WIDTH: u32 = 128;
129
130    const CHARACTER_SIZE: Size = Size::new(4, 6);
131
132    fn char_offset(c: char) -> u32 {
133        if c <= '\u{7f}' {
134            return (c as u32) * 2;
135        } else if c <= '\u{b3}' {
136            return c as u32 + 0x80;
137        }
138        ('?' as u32) * 2
139    }
140}