package Style;

sub attribOutput {
  my ($self, $attrib, $abstractVal) = @_;

  my $tag = undef;
  my $attribTag = $attrib . 'Tag';
  eval {
    local $SIG{__DIE__} = 'IGNORE'; # else will get [CGI::Carp]ed at
    $tag = $self->$attribTag();
  };

  my $output = undef;
  if (defined $tag) {
    my $val;
    my $attribValue = $attrib . 'Value';
    eval {
      local $SIG{__DIE__} = 'IGNORE'; # else will get [CGI::Carp]ed at
      $val = $self->$attribValue($abstractVal);
    };
    $@ eq '' or $val = $abstractVal;
    if (defined $val) {
      $output = $self->taggedValueOutput($tag, $val);
    }
  }
  else {
    my $attribOutput = $attrib . 'Output';
    eval {
      $output = $self->$attribOutput($val);
    };
  }

  $output;
}

sub attribBlockOutput {
  my $self = shift;
  my @entries = ();
  while (defined (my $attrib = shift)) {
    my $value = shift;
    defined $value or $value = '';
    my $output = $self->attribOutput($attrib, $value);
    push @entries, $output if defined $output;
  }

  $self->blockOutput(@entries);
}

# FIXME by default these translate the values set up in
# [StyleSheet::new] back to [undef] --- this may not really be appropriate

sub font_sizeValue {
  $_[1] == 0 ? undef : $_[1];
}

sub colourValue {
  $_[1] == ' ' ? undef : $_[1];
}

sub shade_percentValue {
  $_[1] == 0 ? undef : $_[1];
}

sub hscale_percentValue {
  $_[1] == 0 ? undef : $_[1];
}

sub vscale_percentValue {
  $_[1] == 0 ? undef : $_[1];
}

sub kernValue {
  $_[1] == 0 ? undef : $_[1];
}

sub trackValue {
  $_[1] == 0 ? undef : $_[1];
}

sub baseshiftValue {
  $_[1] == 0 ? undef : $_[1];
}

1;
