Make Clark Kent write out UTF-8 files. user/paxed/clark-kent-utf8
authorPasi Kallinen <pasi.kallinen@pttk.fi>
Wed, 3 Jul 2013 05:19:38 +0000 (08:19 +0300)
committerPasi Kallinen <pasi.kallinen@pttk.fi>
Wed, 3 Jul 2013 09:19:38 +0000 (12:19 +0300)
Allow giving a font -parameter to change the default GD font for the graphs.
Default to a TTF font, so UTF-8 characters work.

Signed-off-by: Pasi Kallinen <pasi.kallinen@pttk.fi>
Open-ILS/src/reporter/clark-kent.pl

index 29b067d..866c332 100755 (executable)
@@ -28,6 +28,7 @@ use Email::Send;
 
 use open ':utf8';
 
+my $font_file = '/usr/share/fonts/truetype/freefont/FreeSans.ttf';
 
 my ($count, $config, $sleep_interval, $lockfile, $daemon) = (1, 'SYSCONFDIR/opensrf_core.xml', 10, '/tmp/reporter-LOCK');
 
@@ -37,6 +38,7 @@ GetOptions(
        "concurrency=i" => \$count,
        "bootstrap|boostrap=s"  => \$config,
        "lockfile=s"    => \$lockfile,
+       "font"          => \$font_file,
 );
 
 if (-e $lockfile) {
@@ -354,6 +356,17 @@ if ($daemon) {
 
 #-------------------------------------------------------------------
 
+sub set_gd_fonts {
+    my $pic = shift;
+    return if (! -e $font_file);
+    $pic->set_title_font($font_file, 14);
+    $pic->set_legend_font($font_file, 8);
+    $pic->set_x_axis_font($font_file, 8);
+    $pic->set_x_label_font($font_file, 10);
+    $pic->set_y_axis_font($font_file, 8);
+    $pic->set_y_label_font($font_file, 8);
+}
+
 sub send_success {
        my $r = shift;
        open F, $success_template or die "Cannot read '$success_template'";
@@ -408,6 +421,7 @@ sub build_csv {
        return unless ($csv);
        
        my $f = new FileHandle (">$file") or die "Cannot write to '$file'";
+       binmode($f, ':utf8');
 
        $csv->print($f, $r->{column_labels});
        $csv->print($f, $_) for (@{$r->{data}});
@@ -438,6 +452,7 @@ sub build_html {
        my $r = shift;
 
        my $index = new FileHandle (">$file") or die "Cannot write to '$file'";
+       binmode($index, ':utf8');
        
        # index header
        print $index <<"        HEADER";
@@ -475,6 +490,7 @@ sub build_html {
        push @links, "<a class='dim' href='report-data.html.debug.html'>Debugging Info</a>";
 
        my $debug = new FileHandle (">$file.debug.html") or die "Cannot write to '$file.debug.html'";
+       binmode($debug, ':utf8');
        print $debug "<html><head><meta charset='utf-8'><title>DEBUG: $$r{report}{name}</title></head><body>";
 
        {       no warnings;
@@ -497,6 +513,7 @@ sub build_html {
        if ($r->{html_format}) {
                # create the raw output html file
                my $raw = new FileHandle (">$file.raw.html") or die "Cannot write to '$file.raw.html'";
+               binmode($raw, ':utf8');
                print $raw "<html><head><meta charset='utf-8'><title>$$r{report}{name}</title>";
 
                print $raw <<'          CSS';
@@ -595,7 +612,7 @@ sub draw_pie {
                        }
 
                        my $pic = new GD::Graph::pie;
-
+                       set_gd_fonts($pic);
                        $pic->set(
                                label                   => $r->{column_labels}->[$vcol],
                                start_angle             => 180,
@@ -712,7 +729,7 @@ sub draw_bars {
        $h = 0 if ($h < 0);
 
        my $pic = new GD::Graph::bars3d ($w + 250, $h + 500);
-
+       set_gd_fonts($pic);
        $pic->set(
                title                   => $r->{report}{name},
                x_labels_vertical       => 1,
@@ -819,7 +836,7 @@ sub draw_lines {
        $h = 0 if ($h < 0);
 
        my $pic = new GD::Graph::lines3d ($w + 250, $h + 500);
-
+       set_gd_fonts($pic);
        $pic->set(
                title                   => $r->{report}{name},
                x_labels_vertical       => 1,