Remove redundant num_fill_bits fields from Shape

This commit is contained in:
Mike Welsh 2016-09-06 12:10:09 -07:00
parent b04902b4e7
commit f6e159f871
4 changed files with 16 additions and 28 deletions

View File

@ -369,8 +369,6 @@ impl<R: Read> Reader<R> {
Ok(ShapeStyles {
fill_styles: fill_styles,
line_styles: line_styles,
num_fill_bits: self.num_fill_bits,
num_line_bits: self.num_line_bits,
})
}
@ -526,9 +524,6 @@ impl<R: Read> Reader<R> {
}
if shape_version >= 2 && (flags & 0b10000) != 0 {
let new_styles = try!(self.read_shape_styles(shape_version));
// TODO: Is this the proper time to apply the new bit sizes?
self.num_fill_bits = new_styles.num_fill_bits;
self.num_line_bits = new_styles.num_line_bits;
new_style.new_styles = Some(new_styles);
}
Some(ShapeRecord::StyleChange(new_style))
@ -833,21 +828,19 @@ pub mod tests {
#[test]
fn read_shape_styles() {
/*
let shape_styles = ShapeStyles {
fill_styles: vec![],
line_styles: vec![],
num_fill_bits: 0,
num_line_bits: 0,
};
assert_eq!(reader(&[0, 0, 0]).read_shape_styles(1).unwrap(), shape_styles);
*/
let shape_styles = ShapeStyles {
fill_styles: vec![
FillStyle::Color(Color { r: 255, g: 0, b: 0, a: 255 })
],
line_styles: vec![],
num_fill_bits: 1,
num_line_bits: 0,
};
//assert_eq!(reader(&[1, , 00, 0]).read_shape_styles(1).unwrap(), shape_styles);
}

View File

@ -53,8 +53,6 @@ pub fn define_shape() -> TagTestData {
FillStyle::Color(Color { r: 255, g: 0, b: 0, a: 255 })
],
line_styles: vec![],
num_fill_bits: 1,
num_line_bits: 0,
},
shape: vec![
ShapeRecord::StyleChange(StyleChangeData {

View File

@ -202,8 +202,6 @@ pub struct Shape {
pub struct ShapeStyles {
pub fill_styles: Vec<FillStyle>,
pub line_styles: Vec<LineStyle>,
pub num_fill_bits: u8,
pub num_line_bits: u8,
}
#[derive(Debug,PartialEq)]

View File

@ -308,8 +308,6 @@ impl<W: Write> Writer<W> {
try!(writer.write_rectangle(&shape.shape_bounds));
try!(writer.write_shape_styles(&shape.styles, shape.version));
writer.num_fill_bits = shape.styles.num_fill_bits;
writer.num_line_bits = shape.styles.num_line_bits;
for shape_record in &shape.shape {
try!(writer.write_shape_record(shape_record, shape.version));
}
@ -351,8 +349,13 @@ impl<W: Write> Writer<W> {
for line_style in &styles.line_styles {
try!(self.write_line_style(line_style, shape_version));
}
try!(self.write_ubits(4, styles.num_fill_bits as u32));
try!(self.write_ubits(4, styles.num_line_bits as u32));
let num_fill_bits = count_ubits(styles.fill_styles.len() as u32);
let num_line_bits = count_ubits(styles.line_styles.len() as u32);
try!(self.write_ubits(4, num_fill_bits as u32));
try!(self.write_ubits(4, num_line_bits as u32));
self.num_fill_bits = num_fill_bits;
self.num_line_bits = num_line_bits;
Ok(())
}
@ -548,20 +551,16 @@ impl<W: Write> Writer<W> {
}
fn count_ubits(mut n: u32) -> u8 {
if n == 0 {
1
} else {
let mut num_bits = 0;
while n > 0 {
n >>= 1;
num_bits += 1;
}
num_bits
let mut num_bits = 0;
while n > 0 {
n >>= 1;
num_bits += 1;
}
num_bits
}
fn count_sbits(n: i32) -> u8 {
if n == 0 || n == -1 {
if n == -1 {
1
} else if n < 0 {
count_ubits((!n) as u32) + 1
@ -717,7 +716,7 @@ mod tests {
#[test]
fn count_ubits() {
assert_eq!(super::count_ubits(0), 1u8);
assert_eq!(super::count_ubits(0), 0u8);
assert_eq!(super::count_ubits(1u32), 1);
assert_eq!(super::count_ubits(2u32), 2);
assert_eq!(super::count_ubits(0b_00111101_00000000u32), 14);