1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
macro_rules! jump_take( (@unwrap $tape:ident, $position:ident, $offset:expr) => ({ try!($tape.jump($position + $offset as u64)); try!($tape.take()) }); (@unwrap $tape:ident, $position:ident, $count:expr, $offsets:expr) => ( jump_take!(@unwrap $tape, $position, $count, i => $offsets[i]) ); (@unwrap $tape:ident, $position:ident, $count:expr, $i:ident => $iterator:expr) => ({ let mut values = Vec::with_capacity($count as usize); for $i in 0..($count as usize) { try!($tape.jump($position + $iterator as u64)); values.push(try!($tape.take())); } values }); ($tape:ident, $position:ident, $offset:expr) => ( Ok(jump_take!(@unwrap $tape, $position, $offset)) ); ($tape:ident, $position:ident, $count:expr, $offsets:expr) => ( Ok(jump_take!(@unwrap $tape, $position, $count, i => $offsets[i])) ); ($tape:ident, $position:ident, $count:expr, $i:ident => $iterator:expr) => ( Ok(jump_take!(@unwrap $tape, $position, $count, $i => $iterator)) ); ); macro_rules! jump_take_given( (@unwrap $tape:ident, $position:ident, $offset:expr, $parameter:expr) => ({ try!($tape.jump($position + $offset as u64)); try!($tape.take_given($parameter)) }); (@unwrap $tape:ident, $position:ident, $count:expr, $offsets:expr, $parameter:expr) => ( jump_take_given!(@unwrap $tape, $position, $count, i => $offsets[i], $parameter) ); (@unwrap $tape:ident, $position:ident, $count:expr, $i:ident => $iterator:expr, $parameter:expr) => ({ let mut values = Vec::with_capacity($count as usize); for $i in 0..($count as usize) { try!($tape.jump($position + $iterator as u64)); values.push(try!($tape.take_given($parameter))); } values }); ($tape:ident, $position:ident, $offset:expr, $parameter:expr) => ( Ok(jump_take_given!(@unwrap $tape, $position, $offset, $parameter)) ); ($tape:ident, $position:ident, $count:expr, $offsets:expr, $parameter:expr) => ( Ok(jump_take_given!(@unwrap $tape, $position, $count, i => $offsets[i], $parameter)) ); ); macro_rules! jump_take_maybe( (@unwrap $tape:ident, $position:ident, $offset:expr) => ( if $offset > 0 { try!($tape.jump($position + $offset as u64)); Some(try!($tape.take())) } else { None } ); (@unwrap $tape:ident, $position:ident, $count:expr, $i:ident => $iterator:expr) => ({ let mut values = Vec::with_capacity($count as usize); for $i in 0..($count as usize) { if $iterator > 0 { try!($tape.jump($position + $iterator as u64)); values.push(Some(try!($tape.take()))); } else { values.push(None); } } values }); ($tape:ident, $position:ident, $offset:expr) => ( Ok(jump_take_maybe!(@unwrap $tape, $position, $offset)) ); ($tape:ident, $position:ident, $count:expr, $offsets:expr) => ( Ok(jump_take_maybe!(@unwrap $tape, $position, $count, i => $offsets[i])) ); ); macro_rules! raise( ($message:expr) => (return Err(::truetype::Error::new(::std::io::ErrorKind::Other, $message))); ); macro_rules! table { ($(#[$attribute:meta])* pub $name:ident { $($field:ident ($($kind:tt)+) $(= $value:block)* $(|$($argument:tt),+| $body:block)*,)* }) => ( table! { @define $(#[$attribute])* pub $name { $($field ($($kind)+),)* } } table! { @implement pub $name { $($field ($($kind)+) [$($value)*] $(|$($argument),+| $body)*,)* } } ); (@position $(#[$attribute:meta])* pub $name:ident { $($field:ident ($($kind:tt)+) $(= $value:block)* $(|$($argument:tt),+| $body:block)*,)* }) => ( table! { @define $(#[$attribute])* pub $name { $($field ($($kind)+),)* } } table! { @implement @position pub $name { $($field ($($kind)+) [$($value)*] $(|$($argument),+| $body)*,)* } } ); (@define $(#[$attribute:meta])* pub $name:ident { $($field:ident ($kind:ty),)* }) => ( $(#[$attribute])* #[derive(Clone, Debug)] pub struct $name { $(pub $field: $kind,)* } ); (@implement pub $name:ident { $($field:ident ($($kind:tt)+) [$($value:block)*] $(|$($argument:tt),+| $body:block)*,)* }) => ( impl ::truetype::Value for $name { fn read<T: ::truetype::Tape>(tape: &mut T) -> ::truetype::Result<Self> { let mut table: $name = unsafe { ::std::mem::zeroed() }; $({ let value = table!(@read $name, table, tape [] [$($kind)+] [$($value)*] $(|$($argument),+| $body)*); ::std::mem::forget(::std::mem::replace(&mut table.$field, value)); })* Ok(table) } } ); (@implement @position pub $name:ident { $($field:ident ($($kind:tt)+) [$($value:block)*] $(|$($argument:tt),+| $body:block)*,)* }) => ( impl ::truetype::Value for $name { fn read<T: ::truetype::Tape>(tape: &mut T) -> ::truetype::Result<Self> { let position = try!(tape.position()); let mut table: $name = unsafe { ::std::mem::zeroed() }; $({ let value = table!(@read $name, table, tape [position] [$($kind)+] [$($value)*] $(|$($argument),+| $body)*); ::std::mem::forget(::std::mem::replace(&mut table.$field, value)); })* Ok(table) } } ); (@read $name:ident, $this:ident, $tape:ident [$($position:tt)*] [$kind:ty] []) => ( try!($tape.take()) ); (@read $name:ident, $this:ident, $tape:ident [$($position:tt)*] [$kind:ty] [$value:block]) => ({ let value = try!($tape.take()); if value != $value { raise!("found a malformed or unsupported table"); } value }); (@read $name:ident, $this:ident, $tape:ident [] [$kind:ty] [] |$this_:pat, $tape_:pat| $body:block) => ({ #[inline(always)] fn read<T: ::truetype::Tape>($this_: &$name, $tape_: &mut T) -> ::truetype::Result<$kind> $body try!(read(&$this, $tape)) }); (@read $name:ident, $this:ident, $tape:ident [$position:ident] [$kind:ty] [] |$this_:pat, $tape_:pat, $position_:pat| $body:block) => ({ #[inline(always)] fn read<T: ::truetype::Tape>($this_: &$name, $tape_: &mut T, $position_: u64) -> ::truetype::Result<$kind> $body try!(read(&$this, $tape, $position)) }); }