<commit_before>#!/usr/bin/perl 

###############################################################################
##  Quick and still somewhat dirty script, which helps generating a PDF with
##  Tip Toi Raster Codes for a given yaml-file for fast testing the application
##  the PDF is designed for printing with 600 dpi resolution
###############################################################################

package main;  

use strict;
use warnings;

use Data::Dumper; # for debugging
use File::Basename;
use Getopt::Long::Descriptive;
use PDF::API2;
use PDF::API2::Content;

my $VERSION = "1.1";


use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;


######  Retrieve and validate parameters ###############################
########################################################################

## Use Getopt-struct for configuration data

my ($opt, $usage) = describe_options(
  "Generate PDF with OID raster code for a given tttool yaml file\nVersion: $VERSION \n$0 \%o ",
  [ 'yaml|y=s',  "yaml file from/for tttool", { required => 1  } ],
  [ 'pdf|p=s',   "pdf file for output, default same path/basename like yamlfile" ],
  [ 'yamlcode=s', "hidden"],
  [ 'Pageheader|P=s', "Header for each PDF page, default is name of the yaml file"],
  [],
  [ 'verbose|v', "verbose", { hidden => 1}      ],
  [ 'help|h',    "print usage message and exit" ],
  #{'show_defaults'=>1}
);
print($usage->text), exit if $opt->help;

# yaml file is mandatory
if (! -f $$opt{'yaml'}) {
  print "No yaml file given, or yaml file does not exist!";
  exit;
}	
if (! exists $$opt{'pdf'} ) {
	  my($filename, $directories, $suffix) = fileparse($$opt{'yaml'}, ('.yaml','.YAML','.Yaml'));
    $$opt{'pdf'} =$directories . $filename . '.pdf';
}	
if ((exists $$opt{'yamlcode'}) && -f $$opt{'pdf'}) {
	# do some further testing
} else {
    my($filename, $directories, $suffix) = fileparse($$opt{'yaml'}, ('.yaml','.YAML','.Yaml'));
    $$opt{'yamlcode'} =$directories . $filename . '.codes.yaml';
    if (! -f $$opt{'yamlcode'}) {
    	print "Can not find additional code file: $$opt{'yamlcode'}\n";
    }	
}	
if (! exists $$opt{'Pageheader'}) {
  $$opt{'Pageheader'} = basename($$opt{'yaml'});
}	


######  Generate PDF document            ###############################
########################################################################


my $pdf = PDF::API2->new( -file => $$opt{'pdf'} );

### The 14 PDF predefined Fonts, which can be used without external Font resources
### For all other fonts, the libraries need to be compiled with Truetype support, which
### is unfortunately not the case for ActivePerl under Windows :-(
### 
my %font = (
  CourierBold         => $pdf->corefont( 'Courier-Bold',          -encoding => 'latin1' ),
  CourierRoman        => $pdf->corefont( 'Courier',               -encoding => 'latin1' ),
  CourierItalic       => $pdf->corefont( 'Courier-Oblique',       -encoding => 'latin1' ),
  CourierBoldItalic   => $pdf->corefont( 'Courier-BoldOblique',   -encoding => 'latin1' ),
  HelveticaBold       => $pdf->corefont( 'Helvetica-Bold',        -encoding => 'latin1' ),
  HelveticaRoman      => $pdf->corefont( 'Helvetica',             -encoding => 'latin1' ),
  HelveticaItalic     => $pdf->corefont( 'Helvetica-Oblique',     -encoding => 'latin1' ),
  HelveticaBoldItalic => $pdf->corefont( 'Helvetica-BoldOblique', -encoding => 'latin1' ),
  TimesBold           => $pdf->corefont( 'Times-Bold',            -encoding => 'latin1' ),
  TimesRoman          => $pdf->corefont( 'Times',                 -encoding => 'latin1' ),
  TimesItalic         => $pdf->corefont( 'Times-Italic',          -encoding => 'latin1' ),
  TimesBoldItalic     => $pdf->corefont( 'Times-BoldItalic',      -encoding => 'latin1' ),
  Symbol              => $pdf->corefont( 'Symbol',                -encoding => 'latin1' ),
  ZapfDingbats        => $pdf->corefont( 'ZapfDingbats',          -encoding => 'latin1' )
);


sub drawLine {
	my ($line, $fromX, $fromY, $toX, $toY, $color, $width) = @_;
  $line->strokecolor( $color);
  $line->linewidth($width);
  $line->move( $fromX, $fromY );
  $line->line( $toX, $toY );
  $line->stroke;
}


sub NewPage {
	my $pdf=shift;
  my $newpage = $pdf->page;
  $newpage->mediabox('A4');
  $newpage->bleedbox('A4');
  $newpage->cropbox ('A4');

  my $blue_box = $newpage->gfx;
  $blue_box->fillcolor('lightblue');
  $blue_box->rect( 0/mm, 270/mm, 210/mm, 27/mm );
  $blue_box->fill;

  drawLine($blue_box, 0, 270/mm, 210/mm, 270/mm, 'darkblue', 2);

  my $headline_text = $newpage->text;

  $headline_text->font( $font{'TimesBoldItalic'}, 40 / pt );
  $headline_text->fillcolor('white');
  $headline_text->translate( 105 / mm, 275 / mm );
  $headline_text->text_center($$opt{'Pageheader'});
  return $newpage;
}  

my $page;
my $context;
my $text;

## Import OID information from both yaml-files
my %OIDs =TTT4perl::createOIDhash($$opt{'yaml'}, $$opt{'yamlcode'});

# sort OID keys, make sure product-id is the first entry!
my @OIDkeys = sort {return -1 if ($a eq 'product-id');return 1 if ($b eq 'product-id'); $a cmp $b } (keys %OIDs);

while (0 < scalar @OIDkeys) {
	# Seite in PDF anlegen
  $page=NewPage($pdf);
  $context = $page->gfx;
  $text    = $page->text;
  # 36 OID pro Seite ausgeben
  for (my $i=0; $i<36; $i++) {
    # Sicherstellen, dass alle Rasterfelder an Positionen starten,
    # die auch sowohl im 72dpi als auch im 600dpi Raster auf
    # ganzzahligen Werten liegen.  3/72  entsprechen 24/600
    my $OID = shift @OIDkeys;
    my $gdx = $pdf->image_gd(TTT4perl::createOIDraster(1200, 300, $OIDs{$OID}));
    $context->image( $gdx,  (39+(186*($i % 3)))/pt, (699-(int($i / 3)*60))/pt, 144/pt, 36/pt);
    $text->font( $font{'TimesBold'}, 14 / pt );
    $text->fillcolor('black');
    $text->translate( (111+(186*($i % 3)))/pt, (711-(int($i / 3)*60))/pt );
    $text->text_center($OID);

    if (0 == scalar @OIDkeys) {
    	last;
    }	
  }
}

$pdf->save;
$pdf->end();

1;
#############################################

package TTT4perl;

use GD;
use YAML::XS qw(Dump Load LoadFile DumpFile);
my @mapOID;
## OID mapping table between code and print values
BEGIN {
  @mapOID = (
  4..7, 12, 15, 20..23, 28, 36..39, 60, 68..69, 84..85, 100..101, 196..199, 204, 207, 212..215, 223, 228..231, 236, 252, 256..257, 
  259, 272..273, 275, 320..321, 323, 336..337, 339, 384..387, 400..403, 449..451, 465..467, 768..769, 771, 776..779, 784, 792..795, 
  801, 808..809, 816..817, 962..963, 969..971, 979, 985..987, 994, 1001, 1010..1011, 1280, 1297, 1299, 1305, 1329, 1331, 1345, 
  1347, 1360, 1368, 1370, 1392, 1408, 1410, 1425, 1427, 1433, 1435, 1457, 1459, 1473, 1475, 1490, 1498, 1522, 1542, 1548..1549, 
  1551, 1565, 1567, 1596..1597, 1604..1607, 1612..1613, 1615, 1620, 1622, 1628, 1660..1661, 1670, 1676..1679, 1693, 1695, 1724..1725, <commit_msg>wenn es keine codes.yaml gibt wird .yaml genommen<commit_after>#!/usr/bin/perl 

###############################################################################
##  Quick and still somewhat dirty script, which helps generating a PDF with
##  Tip Toi Raster Codes for a given yaml-file for fast testing the application
##  the PDF is designed for printing with 600 dpi resolution
###############################################################################

package main;  

use strict;
use warnings;

use Data::Dumper; # for debugging
use File::Basename;
use Getopt::Long::Descriptive;
use PDF::API2;
use PDF::API2::Content;

my $VERSION = "1.1";


use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;


######  Retrieve and validate parameters ###############################
########################################################################

## Use Getopt-struct for configuration data

my ($opt, $usage) = describe_options(
  "Generate PDF with OID raster code for a given tttool yaml file\nVersion: $VERSION \n$0 \%o ",
  [ 'yaml|y=s',  "yaml file from/for tttool", { required => 1  } ],
  [ 'pdf|p=s',   "pdf file for output, default same path/basename like yamlfile" ],
  [ 'yamlcode=s', "hidden"],
  [ 'Pageheader|P=s', "Header for each PDF page, default is name of the yaml file"],
  [],
  [ 'verbose|v', "verbose", { hidden => 1}      ],
  [ 'help|h',    "print usage message and exit" ],
  #{'show_defaults'=>1}
);
print($usage->text), exit if $opt->help;

# yaml file is mandatory
if (! -f $$opt{'yaml'}) {
  print "No yaml file given, or yaml file does not exist!";
  exit;
}	
if (! exists $$opt{'pdf'} ) {
	  my($filename, $directories, $suffix) = fileparse($$opt{'yaml'}, ('.yaml','.YAML','.Yaml'));
    $$opt{'pdf'} =$directories . $filename . '.pdf';
}	
if ((exists $$opt{'yamlcode'}) && -f $$opt{'pdf'}) {
	# do some further testing
} else {
    my($filename, $directories, $suffix) = fileparse($$opt{'yaml'}, ('.yaml','.YAML','.Yaml'));
    $$opt{'yamlcode'} =$directories . $filename . '.codes.yaml';
    if (! -f $$opt{'yamlcode'}) {
        $$opt{'yamlcode'} =$directories . $filename.'.yaml';
    	print "Can not find additional code file: $$opt{'yamlcode'}\n";
    }	
}	
if (! exists $$opt{'Pageheader'}) {
  $$opt{'Pageheader'} = basename($$opt{'yaml'});
}	


######  Generate PDF document            ###############################
########################################################################


my $pdf = PDF::API2->new( -file => $$opt{'pdf'} );

### The 14 PDF predefined Fonts, which can be used without external Font resources
### For all other fonts, the libraries need to be compiled with Truetype support, which
### is unfortunately not the case for ActivePerl under Windows :-(
### 
my %font = (
  CourierBold         => $pdf->corefont( 'Courier-Bold',          -encoding => 'latin1' ),
  CourierRoman        => $pdf->corefont( 'Courier',               -encoding => 'latin1' ),
  CourierItalic       => $pdf->corefont( 'Courier-Oblique',       -encoding => 'latin1' ),
  CourierBoldItalic   => $pdf->corefont( 'Courier-BoldOblique',   -encoding => 'latin1' ),
  HelveticaBold       => $pdf->corefont( 'Helvetica-Bold',        -encoding => 'latin1' ),
  HelveticaRoman      => $pdf->corefont( 'Helvetica',             -encoding => 'latin1' ),
  HelveticaItalic     => $pdf->corefont( 'Helvetica-Oblique',     -encoding => 'latin1' ),
  HelveticaBoldItalic => $pdf->corefont( 'Helvetica-BoldOblique', -encoding => 'latin1' ),
  TimesBold           => $pdf->corefont( 'Times-Bold',            -encoding => 'latin1' ),
  TimesRoman          => $pdf->corefont( 'Times',                 -encoding => 'latin1' ),
  TimesItalic         => $pdf->corefont( 'Times-Italic',          -encoding => 'latin1' ),
  TimesBoldItalic     => $pdf->corefont( 'Times-BoldItalic',      -encoding => 'latin1' ),
  Symbol              => $pdf->corefont( 'Symbol',                -encoding => 'latin1' ),
  ZapfDingbats        => $pdf->corefont( 'ZapfDingbats',          -encoding => 'latin1' )
);


sub drawLine {
	my ($line, $fromX, $fromY, $toX, $toY, $color, $width) = @_;
  $line->strokecolor( $color);
  $line->linewidth($width);
  $line->move( $fromX, $fromY );
  $line->line( $toX, $toY );
  $line->stroke;
}


sub NewPage {
	my $pdf=shift;
  my $newpage = $pdf->page;
  $newpage->mediabox('A4');
  $newpage->bleedbox('A4');
  $newpage->cropbox ('A4');

  my $blue_box = $newpage->gfx;
  $blue_box->fillcolor('lightblue');
  $blue_box->rect( 0/mm, 270/mm, 210/mm, 27/mm );
  $blue_box->fill;

  drawLine($blue_box, 0, 270/mm, 210/mm, 270/mm, 'darkblue', 2);

  my $headline_text = $newpage->text;

  $headline_text->font( $font{'TimesBoldItalic'}, 40 / pt );
  $headline_text->fillcolor('white');
  $headline_text->translate( 105 / mm, 275 / mm );
  $headline_text->text_center($$opt{'Pageheader'});
  return $newpage;
}  

my $page;
my $context;
my $text;

## Import OID information from both yaml-files
my %OIDs =TTT4perl::createOIDhash($$opt{'yaml'}, $$opt{'yamlcode'});

# sort OID keys, make sure product-id is the first entry!
my @OIDkeys = sort {return -1 if ($a eq 'product-id');return 1 if ($b eq 'product-id'); $a cmp $b } (keys %OIDs);

while (0 < scalar @OIDkeys) {
	# Seite in PDF anlegen
  $page=NewPage($pdf);
  $context = $page->gfx;
  $text    = $page->text;
  # 36 OID pro Seite ausgeben
  for (my $i=0; $i<36; $i++) {
    # Sicherstellen, dass alle Rasterfelder an Positionen starten,
    # die auch sowohl im 72dpi als auch im 600dpi Raster auf
    # ganzzahligen Werten liegen.  3/72  entsprechen 24/600
    my $OID = shift @OIDkeys;
    my $gdx = $pdf->image_gd(TTT4perl::createOIDraster(1200, 300, $OIDs{$OID}));
    $context->image( $gdx,  (39+(186*($i % 3)))/pt, (699-(int($i / 3)*60))/pt, 144/pt, 36/pt);
    $text->font( $font{'TimesBold'}, 14 / pt );
    $text->fillcolor('black');
    $text->translate( (111+(186*($i % 3)))/pt, (711-(int($i / 3)*60))/pt );
#    $text->text_center($OID);

    if (0 == scalar @OIDkeys) {
    	last;
    }	
  }
}

$pdf->save;
$pdf->end();

1;
#############################################

package TTT4perl;

use GD;
use YAML::XS qw(Dump Load LoadFile DumpFile);
my @mapOID;
## OID mapping table between code and print values
BEGIN {
  @mapOID = (
  4..7, 12, 15, 20..23, 28, 36..39, 60, 68..69, 84..85, 100..101, 196..199, 204, 207, 212..215, 223, 228..231, 236, 252, 256..257, 
  259, 272..273, 275, 320..321, 323, 336..337, 339, 384..387, 400..403, 449..451, 465..467, 768..769, 771, 776..779, 784, 792..795, 
  801, 808..809, 816..817, 962..963, 969..971, 979, 985..987, 994, 1001, 1010..1011, 1280, 1297, 1299, 1305, 1329, 1331, 1345, 
  1347, 1360, 1368, 1370, 1392, 1408, 1410, 1425, 1427, 1433, 1435, 1457, 1459, 1473, 1475, 1490, 1498, 1522, 1542, 1548..1549, 
  1551, 1565, 1567, 1596..1597, 1604..1607, 1612..1613, 1615, 1620, 1622, 1628, 1660..1661, 1670, 1676..1679, 1693, 1695, 1724..1725, 
{'fraction_non_alphanumeric': 0.24193079383541727, 'fraction_numerical': 0.0774934574004071, 'mean_word_length': 3.587195731910637, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 163, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7562904', 'n_tokens_mistral': 5786, 'n_tokens_neox': 4881, 'n_words': 1270}