Skip to content

Commit

Permalink
Merge pull request #1411 from mitchmindtree/rusttype-0.9.2
Browse files Browse the repository at this point in the history
Update to rusttype 0.8.3
  • Loading branch information
mitchmindtree authored Apr 14, 2021
2 parents 3f9998c + a473f7c commit 8531889
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 85 deletions.
15 changes: 10 additions & 5 deletions backends/conrod_gfx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use gfx::{

use conrod_core::{
color, image, render,
text::{rt, GlyphCache},
text::{self, rt, GlyphCache},
Rect, Scalar,
};

Expand Down Expand Up @@ -151,6 +151,7 @@ pub struct Renderer<'a, R: Resources> {
data: pipe::Data<R>,
commands: Vec<PreparedCommand>,
vertices: Vec<Vertex>,
positioned_glyphs: Vec<text::PositionedGlyph>,
}

impl<'a, R: Resources> Renderer<'a, R> {
Expand Down Expand Up @@ -218,6 +219,7 @@ impl<'a, R: Resources> Renderer<'a, R> {

(cache, texture, texture_view)
};

Ok(Renderer {
pipeline,
glyph_cache,
Expand All @@ -226,6 +228,7 @@ impl<'a, R: Resources> Renderer<'a, R> {
data,
commands: vec![],
vertices: vec![],
positioned_glyphs: vec![],
})
}

Expand Down Expand Up @@ -257,6 +260,7 @@ impl<'a, R: Resources> Renderer<'a, R> {
let Renderer {
ref mut commands,
ref mut vertices,
ref mut positioned_glyphs,
ref mut glyph_cache,
ref mut cache_tex,
..
Expand Down Expand Up @@ -432,10 +436,11 @@ impl<'a, R: Resources> Renderer<'a, R> {
} => {
switch_to_plain_state!();

let positioned_glyphs = text.positioned_glyphs(dpi_factor as f32);
positioned_glyphs.clear();
positioned_glyphs.extend(text.positioned_glyphs(dpi_factor as f32));

// Queue the glyphs to be cached
for glyph in positioned_glyphs {
for glyph in positioned_glyphs.iter() {
glyph_cache.queue_glyph(font_id.index(), glyph.clone());
}

Expand Down Expand Up @@ -469,8 +474,8 @@ impl<'a, R: Resources> Renderer<'a, R> {
)) * 2.0,
};

for g in positioned_glyphs {
if let Ok(Some((uv_rect, screen_rect))) = glyph_cache.rect_for(cache_id, g)
for g in positioned_glyphs.drain(..) {
if let Ok(Some((uv_rect, screen_rect))) = glyph_cache.rect_for(cache_id, &g)
{
let gl_rect = to_gl_rect(screen_rect);
let v = |p, t| Vertex {
Expand Down
10 changes: 7 additions & 3 deletions backends/conrod_glium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Renderer {
glyph_cache: GlyphCache,
commands: Vec<PreparedCommand>,
vertices: Vec<Vertex>,
positioned_glyphs: Vec<text::PositionedGlyph>,
}

/// An iterator yielding `Command`s, produced by the `Renderer::commands` method.
Expand Down Expand Up @@ -481,6 +482,7 @@ impl Renderer {
glyph_cache: gc,
commands: Vec::new(),
vertices: Vec::new(),
positioned_glyphs: Vec::new(),
})
}

Expand Down Expand Up @@ -508,6 +510,7 @@ impl Renderer {
ref mut commands,
ref mut vertices,
ref mut glyph_cache,
ref mut positioned_glyphs,
..
} = *self;

Expand Down Expand Up @@ -694,7 +697,8 @@ impl Renderer {
} => {
switch_to_plain_state!();

let positioned_glyphs = text.positioned_glyphs(dpi_factor as f32);
positioned_glyphs.clear();
positioned_glyphs.extend(text.positioned_glyphs(dpi_factor as f32));

let GlyphCache {
ref mut cache,
Expand Down Expand Up @@ -765,8 +769,8 @@ impl Renderer {
)) * 2.0,
};

for g in positioned_glyphs {
if let Ok(Some((uv_rect, screen_rect))) = cache.rect_for(cache_id, g) {
for g in positioned_glyphs.drain(..) {
if let Ok(Some((uv_rect, screen_rect))) = cache.rect_for(cache_id, &g) {
let gl_rect = to_gl_rect(screen_rect);
let v = |p, t| Vertex {
position: p,
Expand Down
4 changes: 2 additions & 2 deletions backends/conrod_piston/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn primitive<'a, Img, G, T, C, F>(
.viewport
.map(|v| v.draw_size[0] as f32 / v.window_size[0] as f32)
.unwrap_or(1.0);
let positioned_glyphs = text.positioned_glyphs(dpi_factor);
let positioned_glyphs: Vec<_> = text.positioned_glyphs(dpi_factor).collect();
// Re-orient the context to top-left origin with *y* facing downwards, as the
// `positioned_glyphs` yield pixel positioning.
let context = context
Expand All @@ -167,7 +167,7 @@ pub fn primitive<'a, Img, G, T, C, F>(

let rectangles = positioned_glyphs
.into_iter()
.filter_map(|g| glyph_cache.rect_for(cache_id, g).ok().unwrap_or(None))
.filter_map(|g| glyph_cache.rect_for(cache_id, &g).ok().unwrap_or(None))
.map(|(uv_rect, screen_rect)| {
let rectangle = {
let div_dpi_factor = |s| (s as f32 / dpi_factor as f32) as f64;
Expand Down
2 changes: 1 addition & 1 deletion backends/conrod_wgpu/examples/all_winit_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn main() {
let logo_path = assets.join("images/rust.png");
let rgba_logo_image = image::open(logo_path)
.expect("Couldn't load logo")
.to_rgba();
.to_rgba8();

// Create the GPU texture and upload the image data.
let (logo_w, logo_h) = rgba_logo_image.dimensions();
Expand Down
2 changes: 1 addition & 1 deletion conrod_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ daggy = "0.5"
fnv = "1.0"
num = "0.3"
pistoncore-input = "1.0.0"
rusttype = { version = "0.8", features = ["gpu_cache"] }
rusttype = { version = "0.8.3", features = ["gpu_cache"] }
instant = "0.1"
copypasta = "0.6"
13 changes: 9 additions & 4 deletions conrod_core/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Mesh {
glyph_cache_pixel_buffer: Vec<u8>,
commands: Vec<PreparedCommand>,
vertices: Vec<Vertex>,
positioned_glyphs: Vec<text::PositionedGlyph>,
}

/// Represents the scizzor in pixel coordinates.
Expand Down Expand Up @@ -138,11 +139,13 @@ impl Mesh {
let glyph_cache_pixel_buffer = vec![0u8; gc_width as usize * gc_height as usize];
let commands = vec![];
let vertices = vec![];
let positioned_glyphs = vec![];
Mesh {
glyph_cache,
glyph_cache_pixel_buffer,
commands,
vertices,
positioned_glyphs,
}
}

Expand Down Expand Up @@ -170,6 +173,7 @@ impl Mesh {
ref mut glyph_cache_pixel_buffer,
ref mut commands,
ref mut vertices,
ref mut positioned_glyphs,
} = *self;

commands.clear();
Expand Down Expand Up @@ -342,10 +346,11 @@ impl Mesh {
} => {
switch_to_plain_state!();

let positioned_glyphs = text.positioned_glyphs(dpi_factor as f32);
positioned_glyphs.clear();
positioned_glyphs.extend(text.positioned_glyphs(dpi_factor as f32));

// Queue the glyphs to be cached
for glyph in positioned_glyphs {
for glyph in positioned_glyphs.iter() {
glyph_cache.queue_glyph(font_id.index(), glyph.clone());
}

Expand Down Expand Up @@ -384,8 +389,8 @@ impl Mesh {
)) * 2.0,
};

for g in positioned_glyphs {
if let Ok(Some((uv_rect, screen_rect))) = glyph_cache.rect_for(cache_id, g)
for g in positioned_glyphs.drain(..) {
if let Ok(Some((uv_rect, screen_rect))) = glyph_cache.rect_for(cache_id, &g)
{
let vk_rect = to_vk_rect(screen_rect);
let v = |p, t| Vertex {
Expand Down
40 changes: 11 additions & 29 deletions conrod_core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub struct Primitives<'a> {
window_rect: Rect,
/// A buffer to use for triangulating polygons and lines for the `Triangles`.
triangles: Vec<Triangle<Point>>,
/// The slice of rusttype `PositionedGlyph`s to re-use for the `Text` primitive.
positioned_glyphs: Vec<text::PositionedGlyph>,
}

/// An owned alternative to the `Primitives` type.
Expand All @@ -50,7 +48,6 @@ pub struct OwnedPrimitives {
primitives: Vec<OwnedPrimitive>,
triangles_single_color: Vec<Triangle<Point>>,
triangles_multi_color: Vec<Triangle<ColoredPoint>>,
max_glyphs: usize,
line_infos: Vec<text::line::Info>,
texts_string: String,
}
Expand Down Expand Up @@ -157,7 +154,6 @@ pub enum PrimitiveKind<'a> {
/// We produce this type rather than the `&[PositionedGlyph]`s directly so that we can properly
/// handle "HiDPI" scales when caching glyphs.
pub struct Text<'a> {
positioned_glyphs: &'a mut Vec<text::PositionedGlyph>,
window_dim: Dimensions,
text: &'a str,
line_infos: &'a [text::line::Info],
Expand Down Expand Up @@ -221,7 +217,6 @@ pub struct WalkOwnedPrimitives<'a> {
triangles_multi_color: &'a [Triangle<ColoredPoint>],
line_infos: &'a [text::line::Info],
texts_str: &'a str,
positioned_glyphs: Vec<text::PositionedGlyph>,
}

impl<'a> Text<'a> {
Expand All @@ -236,9 +231,11 @@ impl<'a> Text<'a> {
/// out text. This is because conrod positioning uses a "pixel-agnostic" `Scalar` value
/// representing *perceived* distances for its positioning and layout, rather than pixel
/// values. During rendering however, the pixel density must be known
pub fn positioned_glyphs(self, dpi_factor: f32) -> &'a [text::PositionedGlyph] {
pub fn positioned_glyphs(
self,
dpi_factor: f32,
) -> impl 'a + Iterator<Item = rusttype::PositionedGlyph<'static>> {
let Text {
positioned_glyphs,
window_dim,
text,
line_infos,
Expand All @@ -248,31 +245,29 @@ impl<'a> Text<'a> {
justify,
y_align,
line_spacing,
..
} = self;

// Convert conrod coordinates to pixel coordinates.
let trans_x = |x: Scalar| (x + window_dim[0] / 2.0) * dpi_factor as Scalar;
let trans_y = |y: Scalar| ((-y) + window_dim[1] / 2.0) * dpi_factor as Scalar;
let trans_x = move |x: Scalar| (x + window_dim[0] / 2.0) * dpi_factor as Scalar;
let trans_y = move |y: Scalar| ((-y) + window_dim[1] / 2.0) * dpi_factor as Scalar;

// Produce the text layout iterators.
let line_infos = line_infos.iter().cloned();
let lines = line_infos.clone().map(|info| &text[info.byte_range()]);
let lines = line_infos.clone().map(move |info| &text[info.byte_range()]);
let line_rects =
text::line::rects(line_infos, font_size, rect, justify, y_align, line_spacing);

// Clear the existing glyphs and fill the buffer with glyphs for this Text.
positioned_glyphs.clear();
let scale = text::f32_pt_to_scale(font_size as f32 * dpi_factor);
for (line, line_rect) in lines.zip(line_rects) {
lines.zip(line_rects).flat_map(move |(line, line_rect)| {
let (x, y) = (
trans_x(line_rect.left()) as f32,
trans_y(line_rect.bottom()) as f32,
);
let point = text::rt::Point { x: x, y: y };
positioned_glyphs.extend(font.layout(line, scale, point).map(|g| g.standalone()));
}

positioned_glyphs
font.layout(line, scale, point)
})
}
}

Expand All @@ -293,7 +288,6 @@ impl<'a> Primitives<'a> {
fonts: fonts,
window_rect: Rect::from_xy_dim([0.0, 0.0], window_dim),
triangles: Vec::new(),
positioned_glyphs: Vec::new(),
}
}

Expand All @@ -303,7 +297,6 @@ impl<'a> Primitives<'a> {
ref mut crop_stack,
ref mut depth_order,
ref mut triangles,
ref mut positioned_glyphs,
graph,
theme,
fonts,
Expand Down Expand Up @@ -598,7 +591,6 @@ impl<'a> Primitives<'a> {
let y_align = Align::End;

let text = Text {
positioned_glyphs: positioned_glyphs,
window_dim: window_rect.dim(),
text: &state.string,
line_infos: &state.line_infos,
Expand Down Expand Up @@ -652,7 +644,6 @@ impl<'a> Primitives<'a> {
let mut primitive_triangles_single_color = Vec::new();
let mut primitive_line_infos = Vec::new();
let mut texts_string = String::new();
let mut max_glyphs = 0;

while let Some(Primitive {
id,
Expand Down Expand Up @@ -726,10 +717,6 @@ impl<'a> Primitives<'a> {
..
} = text;

// Keep a rough estimate of the maximum number of glyphs so that we know what
// capacity we should allocate the `PositionedGlyph` buffer with.
max_glyphs = std::cmp::max(max_glyphs, text.len());

// Pack the `texts_string`.
let start_str_byte = texts_string.len();
texts_string.push_str(text);
Expand Down Expand Up @@ -769,7 +756,6 @@ impl<'a> Primitives<'a> {
primitives: primitives,
triangles_single_color: primitive_triangles_single_color,
triangles_multi_color: primitive_triangles_multi_color,
max_glyphs: max_glyphs,
line_infos: primitive_line_infos,
texts_string: texts_string,
}
Expand All @@ -785,15 +771,13 @@ impl OwnedPrimitives {
ref triangles_multi_color,
ref line_infos,
ref texts_string,
max_glyphs,
} = *self;
WalkOwnedPrimitives {
primitives: primitives.iter(),
triangles_single_color: triangles_single_color,
triangles_multi_color: triangles_multi_color,
line_infos: line_infos,
texts_str: texts_string,
positioned_glyphs: Vec::with_capacity(max_glyphs),
}
}
}
Expand All @@ -803,7 +787,6 @@ impl<'a> WalkOwnedPrimitives<'a> {
pub fn next(&mut self) -> Option<Primitive> {
let WalkOwnedPrimitives {
ref mut primitives,
ref mut positioned_glyphs,
triangles_single_color,
triangles_multi_color,
line_infos,
Expand Down Expand Up @@ -869,7 +852,6 @@ impl<'a> WalkOwnedPrimitives<'a> {
let line_infos = &line_infos[line_infos_range.clone()];

let text = Text {
positioned_glyphs: positioned_glyphs,
window_dim: window_dim,
text: text_str,
line_infos: line_infos,
Expand Down
Loading

0 comments on commit 8531889

Please sign in to comment.