Skip to content

Commit

Permalink
Update badge
Browse files Browse the repository at this point in the history
- Remove outdated `stb_truetype` crate
- Parse the font once instead of on every request
  • Loading branch information
jyn514 authored and Joshua Nelson committed Jul 1, 2020
1 parent d18ac61 commit 31f3be3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
59 changes: 22 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/web/badge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ edition = "2018"

[dependencies]
base64 = "0.9.0"
rusttype = "0.7"
rusttype = "0.9"
once_cell = "1"
14 changes: 7 additions & 7 deletions src/web/badge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Simple badge generator
use base64::display::Base64Display;
use rusttype::{point, Font, FontCollection, Point, PositionedGlyph, Scale};
use rusttype::{point, Font, Point, PositionedGlyph, Scale};
use once_cell::sync::OnceCell;

const FONT_DATA: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/DejaVuSans.ttf"));
const FONT_SIZE: f32 = 11.0;
Expand Down Expand Up @@ -34,12 +35,11 @@ pub struct Badge {

impl Badge {
pub fn new(options: BadgeOptions) -> Result<Badge, String> {
let collection = FontCollection::from_bytes(FONT_DATA).expect("Failed to parse FONT_DATA");

// this should never fail in practice
let font = collection
.into_font()
.map_err(|_| "Failed to load font data".to_owned())?;
static FONT: OnceCell<Font> = OnceCell::new();
// Font is an `Arc` and therefore cheap to clone
let font = FONT.get_or_init(|| {
Font::try_from_bytes(FONT_DATA).expect("Failed to parse FONT_DATA")
}).clone();

let scale = Scale {
x: FONT_SIZE,
Expand Down

0 comments on commit 31f3be3

Please sign in to comment.