adding roboto

This commit is contained in:
Dustin Carlino 2019-11-29 10:17:14 -08:00
parent 6c6113d1bc
commit 5fd067ea62
7 changed files with 55 additions and 26 deletions

View File

@ -94,9 +94,13 @@ A/B Street binary releases contain pre-built maps that combine data from:
(https://www.opendatacommons.org/licenses/pddl/1.0/)
- https://github.com/seattleio/seattle-boundaries-data
(https://creativecommons.org/publicdomain/zero/1.0/)
- DejaVuSans.ttf (https://dejavu-fonts.github.io/License.html)
- Puget Sound Regional Council
(https://www.psrc.org/activity-based-travel-model-soundcast)
Other binary data bundled in:
- DejaVuSans.ttf (https://dejavu-fonts.github.io/License.html)
- Roboto-Regular.ttf (https://fonts.google.com/specimen/Roboto, Apache license)
- http://www.textures4photoshop.com for textures
- Icons from https://thenounproject.com/aiga-icons/,
https://thenounproject.com/sakchai.ruankam, https://thenounproject.com/wilmax

View File

@ -1,6 +1,6 @@
use crate::{ScreenDims, Text};
use glium_glyph::glyph_brush::rusttype::{Font, Scale};
use glium_glyph::glyph_brush::GlyphCruncher;
use glium_glyph::glyph_brush::{FontId, GlyphCruncher};
use glium_glyph::{GlyphBrush, GlyphBrushBuilder};
use std::cell::RefCell;
use std::collections::HashMap;
@ -9,7 +9,7 @@ use std::collections::HashMap;
pub struct Assets {
pub screenspace_glyphs: RefCell<GlyphBrush<'static, 'static>>,
pub mapspace_glyphs: RefCell<GlyphBrush<'static, 'static>>,
line_height_per_font_size: RefCell<HashMap<usize, f64>>,
line_height_per_font_size: RefCell<HashMap<(FontId, usize), f64>>,
pub default_line_height: f64,
pub font_size: usize,
}
@ -17,7 +17,14 @@ pub struct Assets {
impl Assets {
pub fn new(display: &glium::Display, font_size: usize) -> Assets {
let dejavu: &[u8] = include_bytes!("assets/DejaVuSans.ttf");
let screenspace_glyphs = GlyphBrush::new(display, vec![Font::from_bytes(dejavu).unwrap()]);
let roboto: &[u8] = include_bytes!("assets/Roboto-Regular.ttf");
let screenspace_glyphs = GlyphBrush::new(
display,
vec![
Font::from_bytes(dejavu).unwrap(),
Font::from_bytes(roboto).unwrap(),
],
);
let mapspace_glyphs = GlyphBrushBuilder::using_font_bytes(dejavu)
.params(glium::DrawParameters {
blend: glium::Blend::alpha_blending(),
@ -37,7 +44,7 @@ impl Assets {
default_line_height: 0.0,
font_size,
};
a.default_line_height = a.line_height(a.font_size);
a.default_line_height = a.line_height(FontId(0), a.font_size);
a
}
@ -46,16 +53,17 @@ impl Assets {
}
// Don't call this while screenspace_glyphs is mutably borrowed.
pub fn line_height(&self, font_size: usize) -> f64 {
pub fn line_height(&self, font: FontId, font_size: usize) -> f64 {
let mut hash = self.line_height_per_font_size.borrow_mut();
if hash.contains_key(&font_size) {
return hash[&font_size];
let key = (font, font_size);
if hash.contains_key(&key) {
return hash[&key];
}
let vmetrics =
self.screenspace_glyphs.borrow().fonts()[0].v_metrics(Scale::uniform(font_size as f32));
let vmetrics = self.screenspace_glyphs.borrow().fonts()[font.0]
.v_metrics(Scale::uniform(font_size as f32));
// TODO This works for this font, but could be more paranoid with abs()
let line_height = f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap);
hash.insert(font_size, line_height);
hash.insert(key, line_height);
line_height
}
}

Binary file not shown.

View File

@ -7,6 +7,7 @@ use crate::{
use geom::{Bounds, Circle, Distance, Line, Polygon, Pt2D};
use glium::uniforms::{SamplerBehavior, SamplerWrapFunction, UniformValue};
use glium::Surface;
use glium_glyph::glyph_brush::FontId;
use std::cell::Cell;
const MAPSPACE: f32 = 0.0;
@ -348,8 +349,8 @@ impl<'a> GfxCtx<'a> {
pub fn default_line_height(&self) -> f64 {
self.assets.default_line_height
}
pub fn line_height(&self, font_size: usize) -> f64 {
self.assets.line_height(font_size)
pub(crate) fn line_height(&self, font: FontId, font_size: usize) -> f64 {
self.assets.line_height(font, font_size)
}
pub fn text_dims(&self, txt: &Text) -> ScreenDims {
self.assets.text_dims(txt)

View File

@ -115,9 +115,6 @@ impl<'a> EventCtx<'a> {
pub fn default_line_height(&self) -> f64 {
self.assets.default_line_height
}
pub fn line_height(&self, font_size: usize) -> f64 {
self.assets.line_height(font_size)
}
pub fn text_dims(&self, txt: &Text) -> ScreenDims {
self.assets.text_dims(txt)
}

View File

@ -2,7 +2,7 @@ use crate::assets::Assets;
use crate::{Canvas, Color, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle};
use geom::{Distance, Polygon, Pt2D};
use glium_glyph::glyph_brush::rusttype::Scale;
use glium_glyph::glyph_brush::GlyphCruncher;
use glium_glyph::glyph_brush::{FontId, GlyphCruncher};
use glium_glyph::glyph_brush::{Section, SectionText, VariedSection};
use textwrap;
@ -17,11 +17,16 @@ pub const INACTIVE_CHOICE_COLOR: Color = Color::grey(0.4);
const MAX_CHAR_WIDTH: f64 = 25.0;
pub const SCALE_DOWN: f64 = 10.0;
// These're hardcoded for simplicity; this list doesn't change much.
const DEJA_VU: FontId = FontId(0);
const ROBOTO: FontId = FontId(1);
#[derive(Debug, Clone)]
pub struct TextSpan {
text: String,
fg_color: Color,
size: Option<usize>,
font: FontId,
// TODO bold, italic, font style
}
@ -37,6 +42,12 @@ impl TextSpan {
self.size = Some(size);
self
}
pub fn roboto(mut self) -> TextSpan {
assert_eq!(self.font, DEJA_VU);
self.font = ROBOTO;
self
}
}
// TODO What's the better way of doing this? Also "Line" is a bit of a misnomer
@ -46,6 +57,7 @@ pub fn Line<S: Into<String>>(text: S) -> TextSpan {
text: text.into(),
fg_color: FG_COLOR,
size: None,
font: DEJA_VU,
}
}
@ -179,7 +191,8 @@ impl Text {
.map(|rect| rect.width())
.unwrap_or(0);
max_width = max_width.max(width);
height += assets.line_height(max_size);
// TODO Assume the same font for all spans
height += assets.line_height(line[0].font, max_size);
}
ScreenDims::new(
self.override_width.unwrap_or_else(|| f64::from(max_width)),
@ -232,7 +245,8 @@ pub fn draw_text_bubble(
.collect(),
..VariedSection::default()
};
let height = g.line_height(max_size);
// TODO Assume the same font for all spans
let height = g.line_height(line[0].font, max_size);
if let Some(c) = line_color {
g.draw_polygon(
@ -297,7 +311,8 @@ pub fn draw_text_bubble_mapspace(
.collect(),
..VariedSection::default()
};
let height = g.line_height(max_size) / SCALE_DOWN;
// TODO Assume the same font for all spans
let height = g.line_height(line[0].font, max_size) / SCALE_DOWN;
if let Some(c) = line_color {
g.draw_polygon(

View File

@ -57,21 +57,20 @@ impl SpeedControls {
);
// Row 3 of labels for the time slider
// TODO Text should be roboto
txt.push((
Text::from(Line("00:00").size(12)).no_bg(),
Text::from(Line("00:00").size(12).roboto()).no_bg(),
ScreenPt::new(25.0, 97.0),
));
let (sunrise_color, sunrise_rect) = ctx.canvas.texture_rect("assets/speed/sunrise.png");
batch.push(sunrise_color, sunrise_rect.translate(94.0, 94.0));
txt.push((
Text::from(Line("12:00").size(12)).no_bg(),
Text::from(Line("12:00").size(12).roboto()).no_bg(),
ScreenPt::new(153.0, 97.0),
));
let (sunset_color, sunset_rect) = ctx.canvas.texture_rect("assets/speed/sunset.png");
batch.push(sunset_color, sunset_rect.translate(220.0, 94.0));
txt.push((
Text::from(Line("24:00").size(12)).no_bg(),
Text::from(Line("24:00").size(12).roboto()).no_bg(),
ScreenPt::new(280.0, 97.0),
));
@ -86,7 +85,7 @@ impl SpeedControls {
.translate(24.0, 128.0),
);
txt.push((
Text::from(Line("speed").size(14)).no_bg(),
Text::from(Line("speed").size(14).roboto()).no_bg(),
ScreenPt::new(32.0, 131.0),
));
@ -328,7 +327,12 @@ impl SpeedControls {
// TODO Center this text
g.draw_text_at_screenspace_topleft(
&Text::from(Line(format!("{:.1}x", self.desired_speed())).size(14)).no_bg(),
&Text::from(
Line(format!("{:.1}x", self.desired_speed()))
.size(14)
.roboto(),
)
.no_bg(),
ScreenPt::new(275.0, 131.0),
);