PDF-Boxer

 view release on metacpan or  search on metacpan

lib/PDF/Boxer/Role/SizePosition.pm  view on Meta::CPAN

has 'border_top'  => ( isa => 'Int', is => 'ro', lazy_build => 1 );

has 'padding_left' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
has 'padding_top'  => ( isa => 'Int', is => 'ro', lazy_build => 1 );

has 'content_left' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
has 'content_top'  => ( isa => 'Int', is => 'ro', lazy_build => 1 );

has 'content_right' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
has 'content_bottom'  => ( isa => 'Int', is => 'rw', lazy_build => 1 );

has 'margin_width'    => ( isa => 'Int', is => 'rw', lazy_build => 1 );
has 'margin_height'   => ( isa => 'Int', is => 'rw', lazy_build => 1 );

has 'border_width'    => ( isa => 'Int', is => 'rw', lazy_build => 1 );
has 'border_height'   => ( isa => 'Int', is => 'rw', lazy_build => 1 );

has 'padding_width'    => ( isa => 'Int', is => 'rw', lazy_build => 1 );
has 'padding_height'   => ( isa => 'Int', is => 'rw', lazy_build => 1 );


has 'grow' => ( isa => 'Bool', is => 'ro', default => 0 );

has 'attribute_rels' => ( isa => 'HashRef', is => 'ro', lazy_build => 1 );

sub _build_attribute_rels{
  return {
    max_width      => [qw! margin_right content_right margin_width border_width padding_width !],
    max_height     => [qw! margin_bottom content_bottom margin_height border_height padding_height!],
    width          => [qw! margin_right content_right margin_width border_width padding_width !],
    height         => [qw! margin_bottom content_bottom margin_height border_height padding_height!],
    margin_left    => [qw! margin_right border_left padding_left content_left !],
    margin_top     => [qw! margin_bottom border_top padding_top content_top content_bottom !],
    margin_right   => [qw! margin_left border_left padding_left content_left content_right !],
    margin_bottom  => [qw! margin_top border_top padding_top content_top content_bottom !],
    margin_width   => [qw! width margin_right content_right border_width padding_width !],
    margin_height  => [qw! height margin_bottom content_bottom border_height padding_height !],
  }
}


sub adjust{
  my ($self, $spec, $sender) = @_;

  foreach my $attr (keys %$spec){
    $self->$attr($spec->{$attr});
    foreach ( @{$self->attribute_rels->{$attr}} ){
      next if $spec->{$_}; # don't clear anything which is in the spec
      my $clear = 'clear_'.$_;
      $self->$clear();
    }
  }

}

sub move{
  my ($self, $x, $y) = @_;
  return if 
    ($self->margin_left && $self->margin_left == $x)
    && ($self->margin_top && $self->margin_top == $y);
  warn $self->name." move $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ margin_left => $x, margin_top => $y });
}

sub set_width{
  my ($self, $arg) = @_;
  return if $self->width && $self->width == $arg;
  warn $self->name." set width $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ width => $arg });
}

sub set_margin_width{
  my ($self, $arg) = @_;
  return if $self->margin_width && $self->margin_width == $arg;
  warn $self->name." set margin_width $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ margin_width => $arg });
}

sub set_height{
  my ($self, $arg) = @_;
  return if $self->height && $self->height == $arg;
  warn $self->name." set height $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ height => $arg });
}

sub set_margin_height{
  my ($self, $arg) = @_;
  return if $self->margin_height && $self->margin_height == $arg;
  warn $self->name." set margin_height $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ margin_height => $arg });
}

sub set_size{
  my ($self, $x, $y) = @_;
  return if 
    ($self->width && $self->width == $x)
    && ($self->height && $self->height == $y);
  warn $self->name." size $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ width => $x, height => $y });
}

sub set_margin_size{
  my ($self, $x, $y) = @_;
  return if $self->margin_width == $x && $self->margin_height == $y;
  warn $self->name." size $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
  $self->adjust({ margin_width => $x, margin_height => $y });
}

sub child_height_set{};
sub child_width_set{};

sub position_set{
  my ($self) = @_;
  return 1 if ($self->has_margin_left || $self->has_margin_right)
           && ($self->has_margin_top || $self->has_margin_bottom);
}

sub size_set{
  my ($self) = @_;
  return 1 if ($self->has_margin_width || $self->has_width)
           && ($self->has_margin_height || $self->has_height);
}

sub _build_margin_left{
  my ($self) = @_;
  if ($self->has_content_left){
    return $self->content_left - $self->padding->[3] - $self->border->[3] - $self->margin->[3];
  } elsif ($self->has_margin_right && $self->has_margin_width){
    return $self->margin_right - $self->margin_width;
  } elsif ($self->has_content_right && $self->has_content_width){
    return $self->content_right - $self->content_width;
  }
  return;
}

sub _build_margin_right{
  my ($self) = @_;
  return $self->margin_left + $self->margin_width;
}

sub _build_margin_top{
  my ($self) = @_;
  if ($self->has_content_top){
    return $self->content_top + $self->padding->[0] + $self->border->[0] + $self->margin->[0];
  } elsif ($self->has_margin_bottom && $self->has_margin_height){
    return $self->margin_bottom - $self->margin_height;
  } elsif ($self->has_content_bottom && $self->has_content_height){
    return $self->content_bottom - $self->content_height;
  }
  return;
}

sub _build_margin_bottom{
  my ($self) = @_;
  return $self->margin_top - $self->margin_height;
}

sub _build_border_left{
  my ($self) = @_;
  return $self->margin_left + $self->margin->[3];
}

sub _build_border_top{
  my ($self) = @_;
  return $self->margin_top - $self->margin->[0];



( run in 1.476 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )