Edition 2018

parent ce06769b
......@@ -6,6 +6,7 @@ authors = [
"Jeremy Soller <jackpot51@gmail.com>",
"Alex Butler <alexheretic@gmail.com>",
]
edition = "2018"
description = """
A pure Rust alternative to libraries like FreeType.
......
#![feature(test)]
#![cfg(feature = "gpu_cache")]
extern crate rusttype;
extern crate test;
#[macro_use]
extern crate lazy_static;
extern crate unicode_normalization;
use rusttype::gpu_cache::*;
use rusttype::*;
......@@ -68,7 +64,7 @@ fn layout_paragraph<'a>(
result
}
lazy_static! {
lazy_static::lazy_static! {
static ref FONTS: Vec<Font<'static>> = vec![
include_bytes!("../fonts/wqy-microhei/WenQuanYiMicroHei.ttf") as &[u8],
include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf") as &[u8],
......
#![feature(test)]
extern crate test;
#[macro_use]
extern crate lazy_static;
extern crate blake2;
extern crate rusttype;
use blake2::{Blake2s, Digest};
use rusttype::*;
lazy_static! {
lazy_static::lazy_static! {
static ref DEJA_VU_MONO: Font<'static> =
Font::from_bytes(include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf") as &[u8]).unwrap();
static ref OPEN_SANS_ITALIC: Font<'static> =
......
extern crate arrayvec;
#[macro_use]
extern crate glium;
extern crate rusttype;
extern crate unicode_normalization;
use glium::{glutin, Surface};
use glium::*;
use rusttype::gpu_cache::Cache;
use rusttype::{point, vector, Font, PositionedGlyph, Rect, Scale};
use std::borrow::Cow;
......@@ -56,11 +50,11 @@ fn main() -> Result<(), Box<Error>> {
let font_data = include_bytes!("../fonts/wqy-microhei/WenQuanYiMicroHei.ttf");
let font = Font::from_bytes(font_data as &[u8])?;
let window = glutin::WindowBuilder::new()
let window = glium::glutin::WindowBuilder::new()
.with_dimensions((512, 512).into())
.with_title("RustType GPU cache example");
let context = glutin::ContextBuilder::new().with_vsync(true);
let mut events_loop = glutin::EventsLoop::new();
let context = glium::glutin::ContextBuilder::new().with_vsync(true);
let mut events_loop = glium::glutin::EventsLoop::new();
let display = glium::Display::new(window, context, &events_loop)?;
let dpi_factor = display.gl_window().get_hidpi_factor();
......@@ -132,7 +126,7 @@ You can also try resizing this window."
let mut finished = false;
events_loop.poll_events(|event| {
use glutin::*;
use glium::glutin::*;
if let Event::WindowEvent { event, .. } = event {
match event {
......
extern crate image;
extern crate rusttype;
use image::{DynamicImage, Rgba};
use rusttype::{point, Font, Scale};
......
extern crate rusttype;
use rusttype::{point, FontCollection, PositionedGlyph, Scale};
use std::io::Write;
......@@ -33,7 +31,7 @@ fn main() {
let offset = point(0.0, v_metrics.ascent);
// Glyphs to draw for "RustType". Feel free to try other strings.
let glyphs: Vec<PositionedGlyph> = font.layout("RustType", scale, offset).collect();
let glyphs: Vec<PositionedGlyph<'_>> = font.layout("RustType", scale, offset).collect();
// Find the most visually pleasing width to display
let width = glyphs
......
......@@ -68,20 +68,13 @@
//! # Ok(())
//! # }
//! ```
extern crate crossbeam_deque;
extern crate crossbeam_utils;
extern crate linked_hash_map;
extern crate num_cpus;
extern crate rustc_hash;
use self::linked_hash_map::LinkedHashMap;
use self::rustc_hash::{FxHashMap, FxHasher};
use linked_hash_map::LinkedHashMap;
use rustc_hash::{FxHashMap, FxHasher};
use std::collections::{HashMap, HashSet};
use std::error;
use std::fmt;
use std::hash::BuildHasherDefault;
use {point, vector, GlyphId, Point, PositionedGlyph, Rect, Vector};
use crate::{point, vector, GlyphId, Point, PositionedGlyph, Rect, Vector};
type FxBuildHasher = BuildHasherDefault<FxHasher>;
......@@ -150,7 +143,7 @@ impl ByteArray2d {
}
}
impl ::std::ops::Index<(usize, usize)> for ByteArray2d {
impl std::ops::Index<(usize, usize)> for ByteArray2d {
type Output = u8;
#[inline]
......@@ -159,7 +152,7 @@ impl ::std::ops::Index<(usize, usize)> for ByteArray2d {
}
}
impl ::std::ops::IndexMut<(usize, usize)> for ByteArray2d {
impl std::ops::IndexMut<(usize, usize)> for ByteArray2d {
#[inline]
fn index_mut(&mut self, (row, col): (usize, usize)) -> &mut u8 {
let vec_index = self.get_vec_index(row, col);
......@@ -773,7 +766,7 @@ impl<'font> Cache<'font> {
if self.multithread && glyph_count > 1 {
// multithread rasterization
use self::crossbeam_deque::{Pop, Steal};
use crossbeam_deque::{Pop, Steal};
use std::{
mem,
sync::mpsc::{self, TryRecvError},
......@@ -915,9 +908,9 @@ impl<'font> Cache<'font> {
}
#[inline]
fn draw_glyph<'font>(
fn draw_glyph(
tex_coords: Rect<u32>,
glyph: &PositionedGlyph<'font>,
glyph: &PositionedGlyph<'_>,
pad_glyphs: bool,
) -> ByteArray2d {
let mut pixels = ByteArray2d::zeros(tex_coords.height() as usize, tex_coords.width() as usize);
......@@ -939,7 +932,8 @@ fn draw_glyph<'font>(
#[cfg(test)]
mod test {
use super::*;
use {Font, FontCollection, Scale};
use crate::{Font, FontCollection, Scale};
use approx::*;
#[test]
fn cache_test() {
......
......@@ -99,17 +99,6 @@
#![cfg_attr(feature = "bench", feature(test))]
#[cfg(feature = "bench")]
extern crate test;
#[cfg(test)]
extern crate unicode_normalization;
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate approx;
extern crate arrayvec;
extern crate ordered_float;
extern crate stb_truetype;
mod geometry;
mod rasterizer;
......@@ -157,8 +146,8 @@ pub struct Font<'a> {
info: tt::FontInfo<SharedBytes<'a>>,
}
impl<'a> fmt::Debug for Font<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for Font<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Font")
}
}
......@@ -246,7 +235,7 @@ impl<'a, T: AsRef<[u8]>> From<&'a T> for SharedBytes<'a> {
#[test]
fn lazy_static_shared_bytes() {
lazy_static! {
lazy_static::lazy_static! {
static ref BYTES: Vec<u8> = vec![0, 1, 2, 3];
}
let shared_bytes: SharedBytes<'static> = (&*BYTES).into();
......@@ -273,8 +262,8 @@ pub struct Glyph<'a> {
inner: GlyphInner<'a>,
}
impl<'a> fmt::Debug for Glyph<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for Glyph<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Glyph").field("id", &self.id().0).finish()
}
}
......@@ -352,8 +341,8 @@ pub struct ScaledGlyph<'a> {
scale: Vector<f32>,
}
impl<'a> fmt::Debug for ScaledGlyph<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for ScaledGlyph<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ScaledGlyph")
.field("id", &self.id().0)
.field("scale", &self.api_scale)
......@@ -371,8 +360,8 @@ pub struct PositionedGlyph<'a> {
bb: Option<Rect<i32>>,
}
impl<'a> fmt::Debug for PositionedGlyph<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for PositionedGlyph<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PositionedGlyph")
.field("id", &self.id().0)
.field("scale", &self.scale())
......@@ -411,21 +400,21 @@ impl Scale {
pub trait IntoGlyphId {
/// Convert `self` into a `GlyphId`, consulting the index map of `font` if
/// necessary.
fn into_glyph_id(self, _: &Font) -> GlyphId;
fn into_glyph_id(self, _: &Font<'_>) -> GlyphId;
}
impl IntoGlyphId for char {
fn into_glyph_id(self, font: &Font) -> GlyphId {
fn into_glyph_id(self, font: &Font<'_>) -> GlyphId {
GlyphId(font.info.find_glyph_index(self as u32))
}
}
impl IntoGlyphId for Codepoint {
fn into_glyph_id(self, font: &Font) -> GlyphId {
fn into_glyph_id(self, font: &Font<'_>) -> GlyphId {
GlyphId(font.info.find_glyph_index(self.0))
}
}
impl IntoGlyphId for GlyphId {
#[inline]
fn into_glyph_id(self, _font: &Font) -> GlyphId {
fn into_glyph_id(self, _font: &Font<'_>) -> GlyphId {
self
}
}
......@@ -564,14 +553,14 @@ impl<'a> Font<'a> {
/// points or glyph ids produced by the given iterator `itr`.
///
/// This is equivalent in behaviour to `itr.map(|c| font.glyph(c))`.
pub fn glyphs_for<I: Iterator>(&self, itr: I) -> GlyphIter<I>
pub fn glyphs_for<I: Iterator>(&self, itr: I) -> GlyphIter<'_, I>
where
I::Item: IntoGlyphId,
{
GlyphIter { font: self, itr }
}
/// Returns an iterator over the names for this font.
pub fn font_name_strings(&self) -> tt::FontNameIter<SharedBytes<'a>> {
pub fn font_name_strings(&self) -> tt::FontNameIter<'_, SharedBytes<'a>> {
self.info.get_font_name_strings()
}
/// A convenience function for laying out glyphs for a string horizontally.
......@@ -620,12 +609,12 @@ impl<'a> Font<'a> {
/// })
/// # ;
/// ```
pub fn layout<'b, 'c>(
&'b self,
s: &'c str,
pub fn layout<'b>(
&self,
s: &'b str,
scale: Scale,
start: Point<f32>,
) -> LayoutIter<'b, 'c> {
) -> LayoutIter<'_, 'b> {
LayoutIter {
font: self,
chars: s.chars(),
......@@ -1073,7 +1062,7 @@ pub enum Error {
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
f.write_str(std::error::Error::description(self))
}
}
......
use crate::geometry::*;
use arrayvec;
use ordered_float::OrderedFloat;
use approx::relative_eq;
trait SliceUp: Sized {
type PerSlice: Iterator<Item = Self>;
......
extern crate rusttype;
use rusttype::*;
static ROBOTO_REGULAR: &[u8] = include_bytes!("../fonts/Roboto-Regular.ttf");
......
extern crate image;
extern crate rusttype;
#[macro_use]
extern crate lazy_static;
use image::{DynamicImage, LumaA};
use rusttype::{point, Font, Scale, ScaledGlyph};
use std::io::Cursor;
lazy_static! {
lazy_static::lazy_static! {
static ref DEJA_VU_MONO: Font<'static> =
Font::from_bytes(include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf") as &[u8]).unwrap();
static ref OPEN_SANS_ITALIC: Font<'static> =
Font::from_bytes(include_bytes!("../fonts/opensans/OpenSans-Italic.ttf") as &[u8]).unwrap();
}
fn draw_luma_alpha(glyph: ScaledGlyph) -> image::GrayAlphaImage {
fn draw_luma_alpha(glyph: ScaledGlyph<'_>) -> image::GrayAlphaImage {
let glyph = glyph.positioned(point(0.0, 0.0));
let bounds = glyph.pixel_bounding_box().unwrap();
let mut glyph_image =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment