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.
This commit is contained in:
Joe Wilm 2017-12-19 18:59:45 -08:00 committed by Joe Wilm
parent c9abf571f2
commit 41296a555e
1 changed files with 38 additions and 42 deletions

View File

@ -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<Atlas>,
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<Atlas>, 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)
}
}