From 82eace809bc2e224b69d3e3193c73a79f6c6e66a Mon Sep 17 00:00:00 2001 From: Manuel Reinhardt Date: Sat, 18 Jan 2020 14:01:30 +0100 Subject: [PATCH] Fix #23 (thanks RazrFalcon!) Also adds tests to ensure that GlyphBuffer serialization works correctly. Hopefully harfbuzz wont introduce format changes... --- src/buffer.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/buffer.rs b/src/buffer.rs index b7edfd2..fb8ebab 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -322,7 +322,7 @@ impl<'a> Read for BufferSerializer<'a> { match self.bytes.read(buf) { // if `bytes` is empty refill it Ok(0) => { - if self.start >= self.end.saturating_sub(1) { + if self.start > self.end.saturating_sub(1) { return Ok(0); } let mut bytes_written = 0; @@ -781,6 +781,7 @@ impl fmt::Display for GlyphBuffer { mod tests { use super::*; use crate::tests::assert_memory_layout_equal; + use crate::{shape, Face, Font}; #[test] fn test_memory_layouts() { @@ -813,4 +814,59 @@ mod tests { let string = "Test String"; UnicodeBuffer::new().add_str_item(&string[4..], &string[0..5]); } + + #[test] + fn test_glyph_buffer_serialization_single_char() { + let path = "testfiles/SourceSansVariable-Roman.ttf"; + let face = Face::from_file(path, 0).unwrap(); + let font = Font::new(face); + let buffer = UnicodeBuffer::new().add_str("A"); + let glyph_buffer = shape(&font, buffer, &[]); + + // serializes only glyph indices + let mut serializer = glyph_buffer.serializer( + Some(&font), + SerializeFormat::Text, + SerializeFlags::NO_ADVANCES + | SerializeFlags::NO_CLUSTERS + | SerializeFlags::NO_POSITIONS + | SerializeFlags::NO_GLYPH_NAMES, + ); + let mut string = String::new(); + serializer.read_to_string(&mut string).unwrap(); + assert_eq!( + string.parse::().unwrap(), + glyph_buffer.get_glyph_infos()[0].codepoint + ); + } + + #[test] + fn test_glyph_buffer_serialization_text() { + let path = "testfiles/SourceSansVariable-Roman.ttf"; + let face = Face::from_file(path, 0).unwrap(); + let font = Font::new(face); + let buffer = UnicodeBuffer::new().add_str("Hello 🌍"); + let glyph_buffer = shape(&font, buffer, &[]); + + // serializes only glyph indices + let mut serializer = glyph_buffer.serializer( + Some(&font), + SerializeFormat::Text, + SerializeFlags::NO_ADVANCES + | SerializeFlags::NO_CLUSTERS + | SerializeFlags::NO_POSITIONS + | SerializeFlags::NO_GLYPH_NAMES, + ); + let mut string = String::new(); + serializer.read_to_string(&mut string).unwrap(); + for (serialized_glyph, glyph_info) in string + .split_terminator('|') + .zip(glyph_buffer.get_glyph_infos()) + { + assert_eq!( + serialized_glyph.parse::().unwrap(), + glyph_info.codepoint + ); + } + } }