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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use swf_tree as ast;
use nom::IResult;
use nom::{le_f32 as parse_le_f32, be_u16 as parse_be_u16, be_u32 as parse_be_u32, le_u8 as parse_u8, le_u32 as parse_le_u32};
use crate::parsers::basic_data_types::{parse_le_fixed8_p8, parse_le_fixed16_p16, parse_straight_s_rgba8};

#[allow(unused_variables)]
pub fn parse_blend_mode(input: &[u8]) -> IResult<&[u8], ast::BlendMode> {
  switch!(input, parse_u8,
    0 => value!(ast::BlendMode::Normal) |
    1 => value!(ast::BlendMode::Normal) |
    2 => value!(ast::BlendMode::Layer) |
    3 => value!(ast::BlendMode::Multiply) |
    4 => value!(ast::BlendMode::Screen) |
    5 => value!(ast::BlendMode::Lighten) |
    6 => value!(ast::BlendMode::Darken) |
    7 => value!(ast::BlendMode::Difference) |
    8 => value!(ast::BlendMode::Add) |
    9 => value!(ast::BlendMode::Subtract) |
    10 => value!(ast::BlendMode::Invert) |
    11 => value!(ast::BlendMode::Alpha) |
    12 => value!(ast::BlendMode::Erase) |
    13 => value!(ast::BlendMode::Overlay) |
    14 => value!(ast::BlendMode::Hardlight)
    // TODO(demurgos): Error on unexpected value
  )
}

pub fn parse_clip_actions_string(input: &[u8], extended_events: bool) -> IResult<&[u8], Vec<ast::ClipAction>> {
  let mut result: Vec<ast::ClipAction> = Vec::new();
  let mut current_input = input;

  // TODO: Use `parse_le_u32` and `parse_le_u16`
  loop {
    let head = if extended_events {
      parse_be_u32(current_input)
    } else {
      map!(current_input, parse_be_u16, |x| x as u32)
    };

    match head {
      Ok((next_input, event_flags)) => {
        if event_flags == 0 {
          current_input = next_input;
          break;
        }
      }
      Err(e) => return Err(e),
    };

    match parse_clip_actions(current_input, extended_events) {
      Ok((next_input, clip_actions)) => {
        result.push(clip_actions);
        current_input = next_input;
      }
      Err(e) => return Err(e),
    };
  }

  Ok((current_input, result))
}

#[allow(unused_variables)]
pub fn parse_clip_event_flags(input: &[u8], extended_events: bool) -> IResult<&[u8], ast::ClipEventFlags> {
  do_parse!(
    input,
    flags: switch!(value!(extended_events),
      true => call!(parse_be_u32) |
      false => map!(parse_be_u16, |x| (x as u32) << 16)
    ) >>
    (ast::ClipEventFlags {
      key_up: (flags & (1 << 31)) != 0,
      key_down: (flags & (1 << 30)) != 0,
      mouse_up: (flags & (1 << 29)) != 0,
      mouse_down: (flags & (1 << 28)) != 0,
      mouse_move: (flags & (1 << 27)) != 0,
      unload: (flags & (1 << 26)) != 0,
      enter_frame: (flags & (1 << 25)) != 0,
      load: (flags & (1 << 24)) != 0,
      drag_over: (flags & (1 << 23)) != 0,
      roll_out: (flags & (1 << 22)) != 0,
      roll_over: (flags & (1 << 21)) != 0,
      release_outside: (flags & (1 << 20)) != 0,
      release: (flags & (1 << 19)) != 0,
      press: (flags & (1 << 18)) != 0,
      initialize: (flags & (1 << 17)) != 0,
      data: (flags & (1 << 16)) != 0,
      construct: (flags & (1 << 10)) != 0,
      key_press: (flags & (1 << 9)) != 0,
      drag_out: (flags & (1 << 8)) != 0,
    })
  )
}

pub fn parse_clip_actions(input: &[u8], extended_events: bool) -> IResult<&[u8], ast::ClipAction> {
  do_parse!(
    input,
    events: apply!(parse_clip_event_flags, extended_events) >>
    actions_size: map!(parse_le_u32, |x| if events.key_press && x > 0 { x - 1 } else { x } as usize) >>
    key_code: cond!(events.key_press, parse_u8) >>
    actions: take!(actions_size) >>
    (ast::ClipAction {
      events: events,
      key_code: key_code,
      actions: actions.to_vec(),
    })
  )
}

pub fn parse_filter_list(input: &[u8]) -> IResult<&[u8], Vec<ast::Filter>> {
  length_count!(input, parse_u8, parse_filter)
}

#[allow(unused_variables)]
pub fn parse_filter(input: &[u8]) -> IResult<&[u8], ast::Filter> {
  switch!(input, parse_u8,
    0 => map!(parse_drop_shadow_filter, |f| ast::Filter::DropShadow(f)) |
    1 => map!(parse_blur_filter, |f| ast::Filter::Blur(f)) |
    2 => map!(parse_glow_filter, |f| ast::Filter::Glow(f)) |
    3 => map!(parse_bevel_filter, |f| ast::Filter::Bevel(f)) |
    4 => map!(parse_gradient_glow_filter, |f| ast::Filter::GradientGlow(f)) |
    5 => map!(parse_convolution_filter, |f| ast::Filter::Convolution(f)) |
    6 => map!(parse_color_matrix_filter, |f| ast::Filter::ColorMatrix(f)) |
    7 => map!(parse_gradient_bevel_filter, |f| ast::Filter::GradientBevel(f))
    // TODO(demurgos): Error on unexpected value
  )
}

pub fn parse_bevel_filter(input: &[u8]) -> IResult<&[u8], ast::filters::Bevel> {
  do_parse!(
    input,
    shadow_color: parse_straight_s_rgba8 >>
    highlight_color: parse_straight_s_rgba8 >>
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    angle: parse_le_fixed16_p16 >>
    distance: parse_le_fixed16_p16 >>
    strength: parse_le_fixed8_p8 >>
    flags: parse_u8 >>
    passes: value!(flags & 0b1111) >>
    on_top: value!((flags & (1 << 4)) != 0) >>
    composite_source: value!((flags & (1 << 5)) != 0) >>
    knockout: value!((flags & (1 << 6)) != 0) >>
    inner: value!((flags & (1 << 7)) != 0) >>
    (ast::filters::Bevel {
      shadow_color: shadow_color,
      highlight_color: highlight_color,
      blur_x: blur_x,
      blur_y: blur_y,
      angle: angle,
      distance: distance,
      strength: strength,
      inner: inner,
      knockout: knockout,
      composite_source: composite_source,
      on_top: on_top,
      passes: passes,
    })
  )
}

pub fn parse_blur_filter(input: &[u8]) -> IResult<&[u8], ast::filters::Blur> {
  do_parse!(
    input,
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    flags: parse_u8 >>
    // Skip bits [0, 2]
    passes: value!(flags >> 3) >>
    (ast::filters::Blur {
      blur_x: blur_x,
      blur_y: blur_y,
      passes: passes,
    })
  )
}

pub fn parse_color_matrix_filter(input: &[u8]) -> IResult<&[u8], ast::filters::ColorMatrix> {
  do_parse!(
    input,
    matrix: length_count!(value!(20), map!(parse_le_f32, |x| x)) >>
    (ast::filters::ColorMatrix {
      matrix: matrix,
    })
  )
}

pub fn parse_convolution_filter(input: &[u8]) -> IResult<&[u8], ast::filters::Convolution> {
  do_parse!(
    input,
    matrix_width: map!(parse_u8, |x| x as usize) >>
    matrix_height: map!(parse_u8, |x| x as usize) >>
    divisor: parse_le_f32 >>
    bias: parse_le_f32 >>
    matrix: length_count!(value!(matrix_width * matrix_height), parse_le_f32) >>
    default_color: parse_straight_s_rgba8 >>
    flags: parse_u8 >>
    preserve_alpha: value!((flags & (1 << 0)) != 0) >>
    clamp: value!((flags & (1 << 1)) != 0) >>
    // Skip bits [2, 7]
    (ast::filters::Convolution {
      matrix_width: matrix_width,
      matrix_height: matrix_height,
      divisor: divisor,
      bias: bias,
      matrix: matrix,
      default_color: default_color,
      clamp: clamp,
      preserve_alpha: preserve_alpha,
    })
  )
}

pub fn parse_drop_shadow_filter(input: &[u8]) -> IResult<&[u8], ast::filters::DropShadow> {
  do_parse!(
    input,
    color: parse_straight_s_rgba8 >>
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    angle: parse_le_fixed16_p16 >>
    distance: parse_le_fixed16_p16 >>
    strength: parse_le_fixed8_p8 >>
    flags: parse_u8 >>
    passes: value!(flags & ((1 << 5) - 1)) >>
    composite_source: value!((flags & (1 << 5)) != 0) >>
    knockout: value!((flags & (1 << 6)) != 0) >>
    inner: value!((flags & (1 << 7)) != 0) >>
    (ast::filters::DropShadow {
      color: color,
      blur_x: blur_x,
      blur_y: blur_y,
      angle: angle,
      distance: distance,
      strength: strength,
      inner: inner,
      knockout: knockout,
      composite_source: composite_source,
      passes: passes,
    })
  )
}

pub fn parse_glow_filter(input: &[u8]) -> IResult<&[u8], ast::filters::Glow> {
  do_parse!(
    input,
    color: parse_straight_s_rgba8 >>
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    strength: parse_le_fixed8_p8 >>
    flags: parse_u8 >>
    passes: value!(flags & ((1 << 5) - 1)) >>
    composite_source: value!((flags & (1 << 5)) != 0) >>
    knockout: value!((flags & (1 << 6)) != 0) >>
    inner: value!((flags & (1 << 7)) != 0) >>
    (ast::filters::Glow {
      color: color,
      blur_x: blur_x,
      blur_y: blur_y,
      strength: strength,
      inner: inner,
      knockout: knockout,
      composite_source: composite_source,
      passes: passes,
    })
  )
}

fn parse_filter_gradient(input: &[u8], color_count: usize) -> IResult<&[u8], Vec<ast::ColorStop>> {
  let mut result: Vec<ast::ColorStop> = Vec::with_capacity(color_count);
  let mut current_input = input;

  for _ in 0..color_count {
    match parse_straight_s_rgba8(current_input) {
      Ok((next_input, color)) => {
        result.push(ast::ColorStop { ratio: 0, color: color });
        current_input = next_input;
      }
      Err(e) => return Err(e),
    };
  }
  for mut color_stop in &mut result {
    match parse_u8(current_input) {
      Ok((next_input, ratio)) => {
        color_stop.ratio = ratio;
        current_input = next_input;
      }
      Err(e) => return Err(e),
    };
  }
  Ok((current_input, result))
}

pub fn parse_gradient_bevel_filter(input: &[u8]) -> IResult<&[u8], ast::filters::GradientBevel> {
  do_parse!(
    input,
    color_count: map!(parse_u8, |x| x as usize) >>
    gradient: apply!(parse_filter_gradient, color_count) >>
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    angle: parse_le_fixed16_p16 >>
    distance: parse_le_fixed16_p16 >>
    strength: parse_le_fixed8_p8 >>
    flags: parse_u8 >>
    passes: value!(flags & ((1 << 4) - 1)) >>
    on_top: value!((flags & (1 << 4)) != 0) >>
    composite_source: value!((flags & (1 << 5)) != 0) >>
    knockout: value!((flags & (1 << 6)) != 0) >>
    inner: value!((flags & (1 << 7)) != 0) >>
    (ast::filters::GradientBevel {
      gradient: gradient,
      blur_x: blur_x,
      blur_y: blur_y,
      angle: angle,
      distance: distance,
      strength: strength,
      inner: inner,
      knockout: knockout,
      composite_source: composite_source,
      on_top: on_top,
      passes: passes,
    })
  )
}

pub fn parse_gradient_glow_filter(input: &[u8]) -> IResult<&[u8], ast::filters::GradientGlow> {
  do_parse!(
    input,
    color_count: map!(parse_u8, |x| x as usize) >>
    gradient: apply!(parse_filter_gradient, color_count) >>
    blur_x: parse_le_fixed16_p16 >>
    blur_y: parse_le_fixed16_p16 >>
    angle: parse_le_fixed16_p16 >>
    distance: parse_le_fixed16_p16 >>
    strength: parse_le_fixed8_p8 >>
    flags: parse_u8 >>
    passes: value!(flags & ((1 << 4) - 1)) >>
    on_top: value!((flags & (1 << 4)) != 0) >>
    composite_source: value!((flags & (1 << 5)) != 0) >>
    knockout: value!((flags & (1 << 6)) != 0) >>
    inner: value!((flags & (1 << 7)) != 0) >>
    (ast::filters::GradientGlow {
      gradient: gradient,
      blur_x: blur_x,
      blur_y: blur_y,
      angle: angle,
      distance: distance,
      strength: strength,
      inner: inner,
      knockout: knockout,
      composite_source: composite_source,
      on_top: on_top,
      passes: passes,
    })
  )
}