Friday, May 25, 2012

Re: Howto print the file containing CJK characters?

#!/usr/bin/perl
use strict;
use warnings;
use utf8;

use Getopt::Long qw/:config no_bundling no_ignore_case/;
use File::Basename qw/fileparse/;

## DEFAULTS setting variables
my @paps_opts = ( '--left-margin=75' ); # Raw paps options
my @dest_opts; # Raw destination opts
my $dest = 'lpr'; # Program to do the printing
my $font = 'DejaVu Sans Mono'; # The font to use
my $size = '9'; # The font size to use
my $header = 1; # Print page headers or not
my $line_nums = 0; # Number lines or not
## End defaults setting variables/ or /
my $pdf;
my $help;

GetOptions(
'paps-opt|o=s{1,2}' => \@paps_opts, # Raw paps options
'dest-opt|O=s{1,2}' => \@dest_opts, # Raw dest options
'destination|d=s' => \$dest,
'pdf|p' => \$pdf,
'line-numbers|l!' => \$line_nums,
'no-line-numbers|L' => sub { $line_nums = 0 },
'font|f=s' => \$font,
'size|s=i' => \$size,
'header|H!' => \$header,
'no-header|N' => sub { $header = 0 },
'usage|u|help|h' => \$help,
);

print <<'END_HELP' and exit if $help;

printme - print text files w/o line numbers through paps and lpr/ps2pdf.

USAGE: printme [OPTIONS] [filenames]

A wrapper around paps|lpr or paps|ps2pdf which
1) adds an option to print line number,
2) by default adds a wider left margin,
3) by default prints page headers (to get page numbers),
4) sets a smaller font size, and
5) lets you set font name and font size independently of each other.

OPTIONS
=======

OPTION ARGUMENT MEANING DEFAULT
-------------------- --------------- ----------------- ----------------
-f, --font '<font name>' Choose font DejaVu Sans Mono

-s, --size <integer> Font size 9

-H, --header none Print headers true

-N, --no-header none ...or don't

-l, --line-numbers none Print line false
numbers

-L, --no-line-numbers none ...or don't

-d, --destination <program> Print handler lpr

-p, --pdf none same as
[Note 1] -d ps2pdf
with output to
'<basename>.pdf'

-o, --paps-opt --<opt> [<val>] Raw paps option --left-margin=75
[Note 2] (repeatable)

-O, --dest-opt --<opt> [<val>] Raw print option
[Note 3] OR -<o> [<val>] (repeatable)

-u, --usage, none Print this help false
-h, --help on the terminal

Notes
-----

[1] That is

$ printme --pdf foo.txt

is equal to

$ printme -d ps2pdf -O - - foo.txt >foo.pdf

or

$ printme -d ps2pdf -O - foo.pdf foo.txt

which have to be used if you want a different PDF filename:

$ printme -d ps2pdf -O - bar.pdf foo.txt

[2] For example

$ printme -o --right-margin 72 foo.txt

[3] For example

$ printme -O -P myotherprinter foo.txt
----
Written by B.P. Jonsson <bpjonsson+printme@gmail.com>

This program comes without any warranty of any kind.
You are free to modify and redistribute it as you see
fit provided you replace the above line with the
name and email address of the author with your own.

END_HELP

push @paps_opts, '--header' if $header;
push @paps_opts, "--font='$font $size'";

$dest = 'ps2pdf' if $pdf;

my( $cur_dir, $temp_dir );
if ( $line_nums ) {
# We need a temp dir to put synonymous files with
# prepended line nums, and we need to chdir there
# to make the paps header look good, and so we need
# to know where we came from to construct good paths to
# output pdf files and to be able to return there.
require File::Spec;
require File::Temp;
$cur_dir = File::Spec->rel2abs(File::Spec->curdir);
$temp_dir = File::Temp->newdir;
}

if ( $line_nums ) {
line_nums( @ARGV );
}
elsif ( ! @ARGV ){
if ( $pdf ) {
# ps2pdf goes from STDIN to STDOUT
push @dest_opts, qw/ - - /;
}
print qx(paps @paps_opts | $dest @dest_opts);
}
else {
do_files( @ARGV );
}

sub do_files {
# Do we have any options?
my( $opt_h ) = @_;
# If yes shift off the options hashref
if ( 'HASH' eq ref $opt_h ) { shift @_ }
# Else make sure we have a dummy options hashref
else { $opt_h = {} }
for my $file ( @_ ) {
my @dest_arg;
if ( $pdf ) {
# Are we writing to STDOUT?
@dest_arg = $opt_h->{stdout} ? qw/ - - / : do {
# ...or to a file?
require File::Basename;
require File::Spec;
# Get the basename of the file
my $base = File::Basename::fileparse($file, qr/\.[^.]*$/);
# Construct the path and name of the now PDF
my $pdf_path = File::Spec->catfile($cur_dir, $base.'.pdf');
# ps2pdf reads from STDIN and writes to the PDF
( '-', $pdf_path );
};
}
print qx(paps @paps_opts $file | $dest @dest_opts @dest_arg);
}
}

sub line_nums {
require IO::File;
# if we are reading from STDIN
unless ( @_ ) {
# Make the paps header appear as it usually would!
my $temp_file = 'stdin';
chdir $temp_dir; # To make header look the same
my $out = IO::File->new($temp_file, '>:encoding(utf8)')
|| die "Temp file $temp_file: $!\n";
while ( <> ) {
# Prepend line numbers
$out->printf('%4i| %s', $., $_);
}
undef $out; # Get rid of the file object, and close file
do_files( { stdout => 1 }, $temp_file );
unlink $temp_file;
chdir $cur_dir; # Return where we came from
return;
}
# elsif @_
for my $file ( @_ ) {
require File::Basename;
# Base name of temp file eq base name of orig file
# to make the paps header look sensible
my $temp_file = File::Basename::fileparse($file);
my $in = IO::File->new($file, '<:encoding(utf8)')
|| die "File $file: $!\n";
chdir $temp_dir; # To make header appear the same
my $out = IO::File->new($temp_file, '>:encoding(utf8)')
|| die "Temp file $temp_file: $!\n";
while ( <$in> ) {
# Prepend line numbers
$out->printf('%4i| %s', $., $_);
}
# Get rid of file objects and close files
for ( $in, $out ) { undef $_ }
do_files( $temp_file );
unlink $temp_file;
chdir $cur_dir; # Return where we came from
}
return;
}

__END__

On 2012-05-23 05:01, Manifolds wrote:
> I find the MacVim can't print the CJK character correctly after
> pressing cmd+P. I don't know which file should be modified to work
> correctly.
> Can MacVim print the line number and change the font size ?
>
> My OS is lion 10.7.4
>

paps is your friend, more exactly paps|lpr

You can do

paps file.txt | lpr

but I don't really like all the paps defaults so I've
written a wrapper around paps|lpr or paps|ps2pdf which
by default adds a wider left margin, always prints page
headers (to get page numbers), sets a smaller font size
and lets you set font name and font size independently
of each other. It also has an option to pipe paps output
to ps2pdf instead of lpr and create sensible PDF file
names while doing so, an option to pipe paps output to
yet something else, and options to pass raw paps options
or lpr/ps2pdf/whatever options.

Last but not least it has an option to prepend line
numbers to the input, which paps can't do by itself.

It's attached. You can set your own defaults near the top,
and read help for the options with perl printme.pl --help


/bpj

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: