Browse Source

* Subtitle::SSA::Record : new type 'z' for parse()

master
Alex 'AdUser' Z 8 years ago
parent
commit
1903967552
  1. 6
      lib/Subtitle/SSA/Event.pm
  2. 8
      lib/Subtitle/SSA/Record.pm

6
lib/Subtitle/SSA/Event.pm

@ -17,9 +17,9 @@ my %FIELDS = (
end => { type => 't', value => 0, name => 'End' },
style => { type => 's', value => 'Default', name => 'Style' },
name => { type => 's', value => '', name => 'Name' },
marginl => { type => 's', value => '0000', name => 'MarginL' },
marginr => { type => 's', value => '0000', name => 'MarginR' },
marginv => { type => 's', value => '0000', name => 'MarginV' },
marginl => { type => 'z', value => 0, name => 'MarginL' },
marginr => { type => 'z', value => 0, name => 'MarginR' },
marginv => { type => 'z', value => 0, name => 'MarginV' },
effect => { type => 's', value => '', name => 'Effect' },
text => { type => 's', value => '', name => 'Text' },
);

8
lib/Subtitle/SSA/Record.pm

@ -51,10 +51,12 @@ sub parse_format_line {
# formats:
# s - string, use as is
# d - decimal, no-zero pad
# b - boolean, 0 as false, -1 as true
# t - timing, like H:MM:SS.MSEC
# f - float, 6.01 or 6 if no fractional part
# x - hex-number, &H00AABBCC for ASS or decimal number for SSA
# special cases:
# b - boolean, 0 as false, -1 as true (used in styles)
# z - same as decimal, but zero-padded to 4 digits (used in dialigue, for offset in pixels)
sub parse {
my ($self, $line, $format) = @_;
$format //= $self->{_format};
@ -78,6 +80,8 @@ sub parse {
my $value = shift @values;
if ($d->{type} eq 'x' and $value =~ m<&H([0-9a-f]{8})>oi) {
$value = hex($1);
} elsif ($d->{type} eq 'z') {
$value = int($value);
} elsif ($d->{type} eq 't') {
$value = parse_timing($value);
return $self->error("can't parse timing: $value")
@ -102,6 +106,8 @@ sub to_string {
$v = sprintf "%.2f", $v;
# hack: make decimal from float if fractional part is zero after round up
$v =~ s{\.00$}{}oi;
} elsif ($d->{type} eq 'z') {
$v = sprintf "%04d", $v;
} elsif ($d->{type} eq 't') {
my ($h, $m, $s, $ms) = make_timing($v);
$v = sprintf("%d:%02d:%02d.%02d", $h, $m, $s, int($ms / 10));

Loading…
Cancel
Save