Browse Source

Make generator even more generic and emits rust modules

master
Marc Poulhiès 2 years ago
parent
commit
389bfd10e1
  1. 21
      data/Makefile
  2. 91
      src/fonts.rs
  3. 109
      src/lib.rs
  4. 47
      src/main.rs

21
data/Makefile

@ -6,17 +6,20 @@ all: ${RAW}
TTFFONT:=Junkyard.ttf
FONTPREFIX:=JunkyardFont
CARGO_ARGS:=run --release --features=exe -- $(TTFFONT) $(FONTPREFIX)
CARGO_ARGS:=run --release --features=exe -- --ttf $(TTFFONT) --pngprefix $(FONTPREFIX) --libfile ../src/fonts.rs
pngs:
cargo $(CARGO_ARGS) 7
cargo $(CARGO_ARGS) 9
cargo $(CARGO_ARGS) 10
cargo $(CARGO_ARGS) 12
cargo $(CARGO_ARGS) 14
cargo $(CARGO_ARGS) 18
cargo $(CARGO_ARGS) 24
cargo $(CARGO_ARGS) 70
echo "use embedded_graphics::{fonts::Font, geometry::Size};" > ../src/fonts.rs
echo "use {char_offset_impl, CHARS_PER_ROW};" >> ../src/fonts.rs
cargo $(CARGO_ARGS) --size 7
cargo $(CARGO_ARGS) --size 9
cargo $(CARGO_ARGS) --size 10
cargo $(CARGO_ARGS) --size 12
cargo $(CARGO_ARGS) --size 14
cargo $(CARGO_ARGS) --size 18
cargo $(CARGO_ARGS) --size 24
cargo $(CARGO_ARGS) --size 70
%.raw: %.png
convert $^ -depth 1 gray:$@

91
src/fonts.rs

@ -0,0 +1,91 @@
use embedded_graphics::{fonts::Font, geometry::Size};
use {char_offset_impl, CHARS_PER_ROW};
/// The 7 point size with a character size of 6x7 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont7Point{}
impl Font for JunkyardFont7Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont7Point.raw");
const CHARACTER_SIZE: Size = Size::new(6, 7);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 9 point size with a character size of 7x9 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont9Point{}
impl Font for JunkyardFont9Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont9Point.raw");
const CHARACTER_SIZE: Size = Size::new(7, 9);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 10 point size with a character size of 8x10 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont10Point{}
impl Font for JunkyardFont10Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont10Point.raw");
const CHARACTER_SIZE: Size = Size::new(8, 10);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 12 point size with a character size of 9x12 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont12Point{}
impl Font for JunkyardFont12Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont12Point.raw");
const CHARACTER_SIZE: Size = Size::new(9, 12);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 14 point size with a character size of 11x14 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont14Point{}
impl Font for JunkyardFont14Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont14Point.raw");
const CHARACTER_SIZE: Size = Size::new(11, 14);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 18 point size with a character size of 13x18 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont18Point{}
impl Font for JunkyardFont18Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont18Point.raw");
const CHARACTER_SIZE: Size = Size::new(13, 18);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 24 point size with a character size of 17x23 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont24Point{}
impl Font for JunkyardFont24Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont24Point.raw");
const CHARACTER_SIZE: Size = Size::new(17, 23);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 70 point size with a character size of 47x66 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont70Point{}
impl Font for JunkyardFont70Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont70Point.raw");
const CHARACTER_SIZE: Size = Size::new(47, 66);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}

109
src/lib.rs

@ -34,10 +34,10 @@
extern crate embedded_graphics;
#[cfg(not(feature = "exe"))]
use embedded_graphics::{fonts::Font, geometry::Size};
const CHARS_PER_ROW: u32 = 32;
#[cfg(not(feature = "exe"))]
const CHARS_PER_ROW: u32 = 32;
pub mod fonts;
#[cfg(not(feature = "exe"))]
fn char_offset_impl(c: char) -> u32 {
@ -53,108 +53,3 @@ fn char_offset_impl(c: char) -> u32 {
}
c as u32 - ' ' as u32 - 33
}
/// The 7 point size with a character size of 5x9 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont7Point {}
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont7Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont7Point.raw");
const CHARACTER_SIZE: Size = Size::new(6, 7);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 9 point size with a character size of 6x11 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont9Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont9Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont9Point.raw");
const CHARACTER_SIZE: Size = Size::new(7, 9);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 10 point size with a character size of 7x13 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont10Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont10Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont10Point.raw");
const CHARACTER_SIZE: Size = Size::new(8, 10);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 12 point size with a character size of 8x15 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont12Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont12Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont12Point.raw");
const CHARACTER_SIZE: Size = Size::new(9, 12);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 14 point size with a character size of 10x18 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont14Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont14Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont14Point.raw");
const CHARACTER_SIZE: Size = Size::new(11, 14);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 18 point size with a character size of 12x22 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont18Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont18Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont18Point.raw");
const CHARACTER_SIZE: Size = Size::new(13, 18);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 24 point size with a character size of 16x30 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont24Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont24Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont24Point.raw");
const CHARACTER_SIZE: Size = Size::new(17, 23);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}
/// The 24 point size with a character size of 16x30 pixels.
#[derive(Debug, Copy, Clone)]
pub struct JunkyardFont70Point;
#[cfg(not(feature = "exe"))]
impl Font for JunkyardFont70Point {
const FONT_IMAGE: &'static [u8] = include_bytes!("../data/JunkyardFont70Point.raw");
const CHARACTER_SIZE: Size = Size::new(47, 66);
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {
char_offset_impl(c)
}
}

47
src/main.rs

@ -17,6 +17,8 @@ mod exe {
extern crate pathfinder_geometry;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::prelude::*;
use self::clap::{App, Arg, ArgMatches};
use self::euclid::Size2D;
@ -39,22 +41,35 @@ mod exe {
.short("d")
.long("dumpglyph");
let libfile_arg = Arg::with_name("LIBFILE")
.help("output file for lib")
.required(true)
.short("l")
.takes_value(true)
.long("libfile");
let ttf_filename_arg = Arg::with_name("TTF")
.help("TTF file name")
.required(true)
.index(1);
.short("t")
.long("ttf")
.takes_value(true);
let png_prefix_arg = Arg::with_name("PNG-PREFIX")
.help("Prefix for PNG")
.required(true)
.index(2);
.short("p")
.long("pngprefix")
.takes_value(true);
let size_arg = Arg::with_name("SIZE")
.help("Font size in blocks")
.default_value("32")
.index(3);
.short("s")
.long("size")
.takes_value(true);
App::new("create-font")
.version("0.1")
.about("Simple tool to create image from TTF for creating font for embedded-graphics`")
.arg(libfile_arg)
.arg(ttf_filename_arg)
.arg(dumpglyph_arg)
.arg(png_prefix_arg)
@ -68,9 +83,12 @@ mod exe {
let ttf_filename = matches.value_of("TTF").unwrap();
let png_prefix = matches.value_of("PNG-PREFIX").unwrap();
let font_size: f32 = matches.value_of("SIZE").unwrap().parse().unwrap();
let dump_glyph_enable = matches.is_present("dumpglyph");
let dump_glyph_enable = matches.is_present("dumpglyph");
let libfilename = matches.value_of("LIBFILE").unwrap();
let mut file = File::open(ttf_filename).unwrap();
let mut libfile = OpenOptions::new().append(true).open(libfilename).unwrap();
let font = Font::from_file(&mut file, 0).expect("error loading font");
@ -153,7 +171,6 @@ mod exe {
)
.expect("error rasterizing glyph");
if dump_glyph_enable {
dump_glyph(glyph, &canvas);
}
@ -184,7 +201,27 @@ mod exe {
}
}
let libfilecontent = format!("
/// The {fsize} point size with a character size of {width}x{heigth} pixels.
#[derive(Debug, Copy, Clone)]
pub struct {prefix}{fsize}Point{{}}
impl Font for {prefix}{fsize}Point {{
const FONT_IMAGE: &'static [u8] = include_bytes!(\"../data/{prefix}{fsize}Point.raw\");
const CHARACTER_SIZE: Size = Size::new({width}, {heigth});
const FONT_IMAGE_WIDTH: u32 = Self::CHARACTER_SIZE.width * CHARS_PER_ROW;
fn char_offset(c: char) -> u32 {{
char_offset_impl(c)
}}
}}",
width = char_size.width,
heigth = char_size.height,
fsize = font_size,
prefix = png_prefix
);
libfile.write_all(libfilecontent.as_bytes()).unwrap();
let filename = format!("{}{}Point.png", png_prefix, font_size);
imgbuf.save(&filename).expect("error saving PNG");
println!("Wrote {} with character size of {}", filename, char_size);
}

Loading…
Cancel
Save