From 41296a555e8df05bb2c83d3356abf0a8d1e5632f Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Tue, 19 Dec 2017 18:59:45 -0800 Subject: [PATCH] Share LoadGlyph implementations Previously there were two separate but intended-to-be-identical implementations. Now the two implementations simply delegate to a single, shared method. This should help correctness issues in the future. --- src/renderer/mod.rs | 80 +++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index d510692c..bb2edf15 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -863,61 +863,57 @@ impl<'a> RenderApi<'a> { } } -impl<'a> LoadGlyph for LoaderApi<'a> { - /// Load a glyph into a texture atlas - /// - /// If the current atlas is full, a new one will be created. - fn load_glyph(&mut self, rasterized: &RasterizedGlyph) -> Glyph { - // At least one atlas is guaranteed to be in the `self.atlas` list; thus - // the unwrap should always be ok. - match self.atlas[*self.current_atlas].insert(rasterized, &mut self.active_tex) { - Ok(glyph) => glyph, - Err(_) => { - *self.current_atlas += 1; - if *self.current_atlas == self.atlas.len() { - let atlas = Atlas::new(ATLAS_SIZE); - *self.active_tex = 0; // Atlas::new binds a texture. Ugh this is sloppy. - self.atlas.push(atlas); - } - self.load_glyph(rasterized) +/// Load a glyph into a texture atlas +/// +/// If the current atlas is full, a new one will be created. +#[inline] +fn load_glyph( + active_tex: &mut GLuint, + atlas: &mut Vec, + current_atlas: &mut usize, + rasterized: &RasterizedGlyph +) -> Glyph { + // At least one atlas is guaranteed to be in the `self.atlas` list; thus + // the unwrap. + match atlas[*current_atlas].insert(rasterized, active_tex) { + Ok(glyph) => glyph, + Err(_) => { + *current_atlas += 1; + if *current_atlas == atlas.len() { + let new = Atlas::new(ATLAS_SIZE); + *active_tex = 0; // Atlas::new binds a texture. Ugh this is sloppy. + atlas.push(new); } + load_glyph(active_tex, atlas, current_atlas, rasterized) } } +} + +#[inline] +fn clear_atlas(atlas: &mut Vec, current_atlas: &mut usize) { + for atlas in atlas.iter_mut() { + atlas.clear(); + } + *current_atlas = 0; +} + +impl<'a> LoadGlyph for LoaderApi<'a> { + fn load_glyph(&mut self, rasterized: &RasterizedGlyph) -> Glyph { + load_glyph(self.active_tex, self.atlas, self.current_atlas, rasterized) + } fn clear(&mut self) { - for atlas in self.atlas.iter_mut() { - atlas.clear(); - } - *self.current_atlas = 0; + clear_atlas(self.atlas, self.current_atlas) } } impl<'a> LoadGlyph for RenderApi<'a> { - /// Load a glyph into a texture atlas - /// - /// If the current atlas is full, a new one will be created. fn load_glyph(&mut self, rasterized: &RasterizedGlyph) -> Glyph { - // At least one atlas is guaranteed to be in the `self.atlas` list; thus - // the unwrap. - match self.atlas[*self.current_atlas].insert(rasterized, &mut self.active_tex) { - Ok(glyph) => glyph, - Err(_) => { - *self.current_atlas += 1; - if *self.current_atlas == self.atlas.len() { - let atlas = Atlas::new(ATLAS_SIZE); - *self.active_tex = 0; // Atlas::new binds a texture. Ugh this is sloppy. - self.atlas.push(atlas); - } - self.load_glyph(rasterized) - } - } + load_glyph(self.active_tex, self.atlas, self.current_atlas, rasterized) } fn clear(&mut self) { - for atlas in self.atlas.iter_mut() { - atlas.clear(); - } - *self.current_atlas = 0; + clear_atlas(self.atlas, self.current_atlas) } }