package Opals::POS;

# Version number
$VERSION   = 0.01;

use strict;
use Encode;
use POSIX qw(
    ceil
    floor
);

#===========================================================================================
#
#============================================================================================

my $maxCharPerline={A=>{normal=>40,double=>20},
                    B=>{normal=>52,double=>26}
                    };
#////////////////////////////////////////////////////////////////////////////

sub new{
    my $self={};
    bless ($self);
    return $self;
}
#////////////////////////////////////////////////////////////////////////////
sub getCmd_linefeed{
    my ($self,$nLines)=@_;
    my $cmd =($nLines >0 && $nLines<=255)?"\x1B\x64" . chr($nLines):"";
    return $cmd;
}
#////////////////////////////////////////////////////////////////////////////
sub formatTxt_center{
    my ($self,$txt,$fs,$marginLeft,$marginRight)=@_;
    my $ret="";

    my $maxCPL=getMaxCPL($fs);
    my $strArr =formatTxt_lines($self,$txt,$fs,$marginLeft,$marginRight);
    foreach my $s(@$strArr){
        my $m =floor(($maxCPL - length($s))/2);
        $s = sprintf("%-$m"."s", "") . $s;
    }
    $ret= join("\r\n",@$strArr);
    return $ret;
}

#////////////////////////////////////////////////////////////////////////////

sub formatTxt {
    my($self,$str,$fs,$marginLeft,$marginRight)=@_;
    my $strArr =formatTxt_lines($self,$str,$fs,$marginLeft,$marginRight);
    return join("\r\n",@$strArr);

}
#////////////////////////////////////////////////////////////////////////////
sub formatTxt_lines {
    my($self,$str,$fs,$marginLeft,$marginRight)=@_;
    my $maxCPL=getMaxCPL($fs) ;
    
    $marginLeft=0  if(!defined $marginLeft );
    $marginRight=0 if(!defined $marginRight );
    
    if($maxCPL >$marginLeft + $marginRight){
        $maxCPL -=$marginLeft + $marginRight;
    }

    my $paddingLeft= sprintf("%-" .$marginLeft ."s" ,"");

    my $ret=[];
    my $r =$str;
    my $c=0;
    
    while(length($r)>0 && length($r) >$maxCPL){
        my $s=substr($r,0,$maxCPL);
        $c=0;
        while($c<10 && substr($r,$maxCPL-$c,1) ne ' '){
            $c++;
        }
        $s=substr($r,0,$maxCPL -$c);
        $r =substr($r,$maxCPL -$c);
        $s =  $paddingLeft . $s;
        $r =~ s/^ +//g;
        push @$ret, $s;
    }
    push @$ret, $paddingLeft .$r;

    return $ret;

}
           
#////////////////////////////////////////////////////////////////////////////
sub getCmd_font {
    my($self,$fs)=@_;
    my $cmd="\x1B\x21";
    my($typeA,$typeB,$bold,$doubleHeight,$doubleWidth,$underline)=(0,1,8,16,32,128);
    my $f=70;

     if($fs->{'type'} eq 'B'){ 
     $f +=$typeB;
     }
     if($fs->{'weight'} eq 'bold'){ 
          $f=$f|$bold  ;
     }
     if($fs->{'height'} eq 'double'){ 
          $f=$f|$doubleHeight  ;
     }
     if($fs->{'width'} eq 'double'){ 
          $f=$f|$doubleWidth  ;
     }
     if($fs->{'underline'} eq 'underline'){ 
          $f=$f|$underline  ;
     }
     $cmd .= chr($f) ;

    return $cmd;
    
    
}
#////////////////////////////////////////////////////////////////////////////
sub getCmd_init{
    my($self)=@_;
    return "\x1B\x40";

}
#////////////////////////////////////////////////////////////////////////////
sub getCmd_cut{
    my($self)=@_;
    return "\x1D\x56\x41";
}
sub getCmd_align{
    my($self,$algin)=@_;
    my $cmd="";
    if($algin eq 'center'){
        $cmd="\x1B\x61\x01";
    }
    elsif($algin eq 'right'){
        $cmd="\x1B\x61\x02";
    }
    else{
        $cmd="\x1B\x40";
    }
    return $cmd;

}

#////////////////////////////////////////////////////////////////////////////
sub getMaxCPL{
    my($fs)=@_;
    my ($type,$width)=("A","normal");
    $type='B' if($fs && $fs->{'type'} eq 'B');
    $width='double' if($fs && $fs->{'width'} eq'double');
    return $maxCharPerline->{$type}->{$width};


}
1;
