Printer-ESCPOS

 view release on metacpan or  search on metacpan

lib/Printer/ESCPOS/Profiles/Generic.pm  view on Meta::CPAN


    if ( $n == 1 ) {
        $self->driver->print( _ESC . '=' . chr(1) );
    }
    elsif ( $n == 0 ) {
        $self->driver->print( _ESC . '=' . chr(2) );
    }
    else {
        confess "Invalid parameter please use '0' or '1'";
    }
}


sub qr {
    my ( $self, $string, $ecc, $version, $moduleSize ) = @_;
    $ecc        ||= 'L';
    $version    ||= 5;
    $moduleSize ||= 3;

    my %eccAllowedValues;
    @eccAllowedValues{qw(L M Q H)} = ();
    confess "Ecc must be one of 'L', 'M', 'Q' or 'H'"
      unless ( exists $eccAllowedValues{$ecc} );
    confess "Version must be between 1 to 40"
      unless ( $version <= 40 and $version >= 1 and $version =~ /\d?\d/ );
    confess "Module size must be between a positive integer"
      unless ( isint $moduleSize == 1 );

    my $qrImage =
      GD::Barcode::QRcode->new( $string,
        { Ecc => $ecc, Version => $version, ModuleSize => $moduleSize } )
      ->plot();
    $self->image($qrImage);
}


sub utf8ImagedText {
    my ( $self, $string, %params ) = @_;
    my $fontFamily = $params{fontFamily} // "Purisa";
    my $fontStyle  = $params{fontStyle}  // "Normal";
    my $fontSize   = $params{fontSize}   // 20;
    my $lineHeight = $params{lineHeight} // 42;
    my $paperWidth = $params{paperWidth} // 500;

    my $surface =
      Cairo::ImageSurface->create( 'argb32', $paperWidth, $lineHeight );
    my $cr = Cairo::Context->create($surface);
    $cr->set_antialias('none');
    $cr->set_source_rgb( 255, 255, 255 );
    $cr->paint();
    $cr->set_source_rgb( 0, 0, 0 );
    my $layout = Pango::Cairo::create_layout($cr);
    $layout->set_text($string);
    my $font =
      Pango::FontDescription->from_string("$fontFamily $fontStyle $fontSize");
    $layout->set_font_description($font);

    Pango::Cairo::show_layout( $cr, $layout );
    my $tempdir = File::Temp::tempdir();
    $surface->write_to_png( $tempdir . '/cairopangoprinterimage.png' );
    my $img = newFromPng GD::Image( $tempdir . '/cairopangoprinterimage.png' )
      || die "Error $!";
    $self->image($img);
}


sub image {
    my ( $self, $img ) = @_;
    my $paddingLeft  = '';
    my $paddingRight = '';

    if ( $img->width > 512 ) {
        carp
'Width is greater than 512 pixels and could be truncated at print time';
    }
    if ( $img->height > 255 ) {
        confess 'Height is greater than 255 pixels';
    }

    my @padding = $self->_pad_image_size( $img->width );
    for ( 1 .. $padding[0] ) {
        $paddingLeft .= '0';
    }
    for ( 1 .. $padding[1] ) {
        $paddingRight .= '0';
    }

    my $pixelLine = '';
    my $switch    = 0;
    my @imageSize = ( 0, 0 );
    for my $y ( 0 .. $img->height - 1 ) {
        $imageSize[1]++;
        $pixelLine .= $paddingLeft;
        $imageSize[0] += $padding[0];
        for my $x ( 0 .. $img->width - 1 ) {
            $imageSize[0]++;
            my $index         = $img->getPixel( $x, $y );
            my @rgb           = $img->rgb($index);
            my $imageColour   = $rgb[0] + $rgb[1] + $rgb[2];
            my $imagePattern  = "1X0";
            my $patternLength = length $imagePattern;
            $switch = ( $switch - 1 ) * (-1);
            for my $x ( 1 .. $patternLength ) {

                if ( $imageColour <= ( 255 * 3 / $patternLength * $x ) ) {
                    my $patternAtX = substr( $imagePattern, $x - 1, 1 );
                    if ( $patternAtX eq 'X' ) {
                        $pixelLine .= $switch;
                    }
                    else {
                        $pixelLine .= $patternAtX;
                    }
                    last;
                }
                elsif (
                    $imageColour > ( 255 * 3 / $patternLength * $patternLength )
                    and $imageColour <= ( 255 * 3 ) )
                {
                    $pixelLine .= substr( $imagePattern, -1, 1 );
                    last;
                }



( run in 2.159 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )