Rechercher dans ce blog

vendredi 19 juin 2009

texttops wrapper iso-8859

Pour pouvoir imprimer des textes non encodés en UTF-8 avec cups et le filter texttops

Il faut wrapper texttops dans ce genre de script qui se charge de faire la conversion de codage

http://www.srz.de/Members/bla/cups/filter/texttops%20wrapper/texttops

#!/usr/bin/perl -w

# texttops.pl wrapper script for the texttops filter

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

# COpyright (C) 2007 Helge Blischke <h.blischke@acm.org>

#

# 1.00 - 2007-12-29/Bl

# initial implementation

# 1.01 - 2007-12-31/Bl

# Options and sugaring

#

# Description:

# ------------

# This script tries to determine the characterset used,

# decodes the input to the internal UTF-8 and writes

# the UTF-8 data to the original texttops filter.

use Encode;

use Encode::Guess;

#

# Configuration data, edit as needed

# -------------------------------------------------------------------------------

$texttops = $ENV{CUPS_SERVERBIN} . '/filter/orig.texttops';

@suspects = qw(iso-8859-15 iso-8859-1); # to try one by one

# -------------------------------------------------------------------------------

if (scalar @ARGV == 0)

{

die "Usage: texttops.pl jobid username title copies options [file]\n";

}

#

# Get the command line parameters

#

$jobid = $username = $title = $copies = undef;

$jobid = shift; # Job ID

$username = shift; # Job requesting user name

$title = shift; # Job title

$copies = shift; # Number of requested copies

$options = shift; # Textual representation of job attributes

$textfile = shift; # Pathname of input text file

#

# Determine from where to read

#

if (defined $textfile)

{

open (FILI, "<:raw", "$textfile") || die "ERROR: $textfile: $!\n";

}

else

{

open (FILI, "<:raw", "&STDIN") || die "ERROR: STDIN: $!\n";

}

#

# First, check the command line options

#

@opts = split (/\s+/, $options);

$charset = undef;

$buffer = undef;

foreach my $opt (@opts)

{

if ($opt =~ /encoding=(\S+)/)

{

$charset = $1;

#$charset =~ tr/A-Z/a-z/; # make lower case

if ($charset =~ /^lang/) # language

{

eval

{

require I18N::Langinfo;

I18N::Langinfo->import(qw(langinfo CODESET));

$charset = langinfo (CODESET ());

};

die "ERROR: $@\n" if ($@); # undefined language

}

else

{

$codec = find_encoding ($charset);

die "ERROR: $charset is not a valid encoding name: $codec\n" unless ref $codec;

}

warn "NOTICE: characterset = $charset\n";

last;

}

}

if (! defined $charset)

{

#

# Read the first buffer and determine the encoding

#

$buffer = '';

$codec = '';

$bytes = read (FILI, $buffer, 16384) || die "ERROR: read input: $!\n";

foreach my $name (@suspects)

{

my $temp = find_encoding ($name);

if (! ref $temp)

{

warn "ERROR: Unknown encoding $name\n";

next;

}

Encode::Guess->set_suspects($name);

$codec = guess_encoding ($buffer);

last if (ref $codec);

warn "NOTICE: $coded\n";

}

ref ($codec) || die "ERROR: Cannot guess: $codec\n";

$charset = $codec->name; # charset name

warn "NOTICE: guessed characterset = $charset\n";

}

#

# Set locale params fore the original texttops filter

#

$language = $ENV{LANG};

$lc_all = $ENV{LC_ALL};

$lc_all = $language if (! defined $lc_all);

$ENV{LC_ALL} = $lc_all;

#

# Open the pipe to the original filter

#

open (FILO, "|$texttops $jobid $username \'$title\' $copies \"$options\"")

|| die "ERROR: Cannot launch original filter: $!\n";

binmode (FILO, ":utf8"); # mark output as UTF-8

if (defined $charset)

{

if (defined $buffer)

{

my $outbuf = $codec->decode ($buffer); # decode the first input buffer

print FILO $outbuf;

}

binmode (FILI, ":encoding($charset)");

}

#

# Now copy the rest of the input over to the original filter

#

while (($bytes = read (FILI, $buffer, 16384)) > 0)

{

print FILO $buffer;

}

close (FILI);

close (FILO);

exit 0;

Aucun commentaire:

Enregistrer un commentaire