| 1 | #!/usr/bin/env perl | ||
| 2 | # -------------------------------------- | ||
| 3 | # | ||
| 4 | # Title: Test PDF::Kit | ||
| 5 | # Purpose: Test PDF::Kit | ||
| 6 | # | ||
| 7 | # Name: pdf_kit_eg | ||
| 8 | # File: pdf_kit_eg | ||
| 9 | # Created: June 18, 2009 | ||
| 10 | # | ||
| 11 | # Copyright: Copyright 2009 by Shawn H. Corey. All rights reserved. | ||
| 12 | # | ||
| 13 | # This program is free software; you can redistribute it and/or modify | ||
| 14 | # it under the terms of the GNU General Public License as published by | ||
| 15 | # the Free Software Foundation, version 3 of the License, or | ||
| 16 | # (at your option) any later version. | ||
| 17 | # | ||
| 18 | # This program is distributed in the hope that it will be useful, | ||
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 21 | # GNU General Public License for more details. | ||
| 22 | # | ||
| 23 | # You should have received a copy of the GNU General Public License | ||
| 24 | # along with this program; if not, write to the Free Software | ||
| 25 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 26 | |||
| 27 | # -------------------------------------- | ||
| 28 | # Pragmas | ||
| 29 | |||
| 30 | require v5.8.0; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | |||
| 35 | use utf8; # Convert all UTF-8 to Perl's internal representation. | ||
| 36 | |||
| 37 | # -------------------------------------- | ||
| 38 | # Version | ||
| 39 | use version; our $VERSION = qv(v1.0.1); | ||
| 40 | |||
| 41 | # -------------------------------------- | ||
| 42 | # Modules | ||
| 43 | use Carp; | ||
| 44 | use Data::Dumper; | ||
| 45 | use English qw( -no_match_vars ) ; # Avoids regex performance penalty | ||
| 46 | use File::Basename; | ||
| 47 | use Getopt::Long; | ||
| 48 | use Pod::Usage; | ||
| 49 | use POSIX; | ||
| 50 | |||
| 51 | use PDF::API2; | ||
| 52 | use PDF::Kit; | ||
| 53 | |||
| 54 | # -------------------------------------- | ||
| 55 | # Configuration Parameters | ||
| 56 | |||
| 57 | my $SPACE = "\x20"; | ||
| 58 | |||
| 59 | my $Box_color = '#0000ff'; | ||
| 60 | |||
| 61 | my $Phrase = 'The quick brown fox jumped over the lazy dogs.'; | ||
| 62 | my $Block = [ in2pts( 1 ), in2pts( 1 ), in2pts( 8.5 - 1 ), in2pts( 11 - 1 ) ]; | ||
| 63 | |||
| 64 | my %P_opts = ( | ||
| 65 | -print_text => \&print_text, | ||
| 66 | -compute_length => \&print_text, | ||
| 67 | -block => $Block, | ||
| 68 | -space_after => 1, | ||
| 69 | |||
| 70 | -size => 20, | ||
| 71 | -indent => 0, | ||
| 72 | -alignment => 0, | ||
| 73 | -justify_word => 0, | ||
| 74 | -justify_char => 0, | ||
| 75 | -justify_scale => 0, | ||
| 76 | ); | ||
| 77 | |||
| 78 | # Make Data::Dumper pretty | ||
| 79 | $Data::Dumper::Sortkeys = 1; | ||
| 80 | $Data::Dumper::Indent = 1; | ||
| 81 | $Data::Dumper::Maxdepth = 0; | ||
| 82 | |||
| 83 | # -------------------------------------- | ||
| 84 | # Variables | ||
| 85 | |||
| 86 | my $Box = 0; | ||
| 87 | |||
| 88 | my ( $Pdf, $Page, $Pen ); | ||
| 89 | my ( $Font_plain, $Font_bold, $Font_italic, $Font_bolditalic ); | ||
| 90 | |||
| 91 | # Documentation levels | ||
| 92 | my $DOC_USAGE = 0; | ||
| 93 | my $DOC_HELP = 1; | ||
| 94 | my $DOC_VER = 2; | ||
| 95 | my $DOC_MAN = 3; | ||
| 96 | |||
| 97 | # -------------------------------------- | ||
| 98 | # Subroutines | ||
| 99 | |||
| 100 | # -------------------------------------- | ||
| 101 | # Name: print_documentation | ||
| 102 | # Usage: print_documentation( $documentation_level ); | ||
| 103 | # Purpose: Print the usage, help, or man documentation. | ||
| 104 | # Returns: Does not return. | ||
| 105 | # Parameters: $documentation_level -- how much documentation to display. | ||
| 106 | # 0 == usage | ||
| 107 | # 1 == help | ||
| 108 | # 2 == version | ||
| 109 | # other == man | ||
| 110 | # | ||
| 111 | sub print_documentation { | ||
| 112 | my $level = shift @_ || 0; | ||
| 113 | |||
| 114 | # print the usage documentation | ||
| 115 | if( $level == $DOC_USAGE ){ | ||
| 116 | pod2usage( | ||
| 117 | -exitval => 2, | ||
| 118 | -verbose => 99, | ||
| 119 | -sections => 'USAGE', | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | # print the help documentation | ||
| 124 | if( $level == $DOC_HELP ){ | ||
| 125 | pod2usage( | ||
| 126 | -exitval => 2, | ||
| 127 | -verbose => 99, | ||
| 128 | -sections => 'NAME|VERSION|USAGE|REQUIRED ARGUMENTS|OPTIONS', | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | # print the version | ||
| 133 | if( $level == $DOC_VER ){ | ||
| 134 | pod2usage( | ||
| 135 | -exitval => 2, | ||
| 136 | -verbose => 99, | ||
| 137 | -sections => 'VERSION', | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | # print the man documentation | ||
| 142 | pod2usage( | ||
| 143 | -exitval => 2, | ||
| 144 | -verbose => 2, | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | |||
| 148 | # -------------------------------------- | ||
| 149 | # Name: get_cmd_opts | ||
| 150 | # Usage: get_cmd_opts(); | ||
| 151 | # Purpose: Process the command-line switches. | ||
| 152 | # Returns: none | ||
| 153 | # Parameters: none | ||
| 154 | # | ||
| 155 | sub get_cmd_opts { | ||
| 156 | |||
| 157 | # Check command-line options | ||
| 158 | unless( GetOptions( | ||
| 159 | box => \$Box, | ||
| 160 | 'alignment=f' => \$P_opts{-alignment}, | ||
| 161 | 'char=f' => \$P_opts{-justify_char}, | ||
| 162 | 'indent=f' => \$P_opts{-indent}, | ||
| 163 | 'scale=f' => \$P_opts{-justify_scale}, | ||
| 164 | 'size=f' => \$P_opts{-size}, | ||
| 165 | 'word=f' => \$P_opts{-justify_word}, | ||
| 166 | |||
| 167 | usage => sub { print_documentation( $DOC_USAGE ); }, | ||
| 168 | help => sub { print_documentation( $DOC_HELP ); }, | ||
| 169 | version => sub { print_documentation( $DOC_VER ); }, | ||
| 170 | man => sub { print_documentation( $DOC_MAN ); }, | ||
| 171 | )){ | ||
| 172 | print_documentation( $DOC_USAGE ); | ||
| 173 | } | ||
| 174 | $P_opts{-indent} = in2pts( $P_opts{-indent} ); | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | # -------------------------------------- | ||
| 179 | # Name: setup | ||
| 180 | # Usage: setup(); | ||
| 181 | # Purpose: Set up the application. | ||
| 182 | # Parameters: none | ||
| 183 | # Returns: none | ||
| 184 | # | ||
| 185 | sub setup { | ||
| 186 | |||
| 187 | $Pdf = PDF::API2->new(); | ||
| 188 | |||
| 189 | $Font_plain = $Pdf->corefont( 'Times-Roman' ); | ||
| 190 | $Font_bold = $Pdf->corefont( 'Times-Bold' ); | ||
| 191 | $Font_italic = $Pdf->corefont( 'Times-Italic' ); | ||
| 192 | $Font_bolditalic = $Pdf->corefont( 'Times-BoldItalic' ); | ||
| 193 | |||
| 194 | $Page = $Pdf->page(); | ||
| 195 | $Pen = $Page->text(); | ||
| 196 | # delete $Pen->{Filter}; # TEMPORARY | ||
| 197 | |||
| 198 | if( $Box ){ | ||
| 199 | my $gfx = $Page->gfx(); | ||
| 200 | $gfx->strokecolor( $Box_color ); | ||
| 201 | $gfx->rectxy( @$Block ); | ||
| 202 | $gfx->stroke(); | ||
| 203 | } | ||
| 204 | |||
| 205 | return; | ||
| 206 | } | ||
| 207 | |||
| 208 | # -------------------------------------- | ||
| 209 | # Name: print_text | ||
| 210 | # Usage: $length = print_text( \%opts, $text ); | ||
| 211 | # Purpose: Print the text. | ||
| 212 | # Parameters: \%opts -- How to | ||
| 213 | # $text -- What to print | ||
| 214 | # Returns: $length -- How much was printed | ||
| 215 | # | ||
| 216 | sub print_text { | ||
| 217 | my $opts = shift @_; | ||
| 218 | my $text = shift @_; | ||
| 219 | my $length = 0; | ||
| 220 | |||
| 221 | if( $opts->{bold} && $opts->{italic} ){ | ||
| 222 | $Pen->font( $Font_bolditalic, $opts->{-size} ); | ||
| 223 | }elsif( $opts->{italic} ){ | ||
| 224 | $Pen->font( $Font_italic, $opts->{-size} ); | ||
| 225 | }elsif( $opts->{bold} ){ | ||
| 226 | $Pen->font( $Font_bold, $opts->{-size} ); | ||
| 227 | }else{ | ||
| 228 | $Pen->font( $Font_plain, $opts->{-size} ); | ||
| 229 | } | ||
| 230 | |||
| 231 | my $wordspace = $Pen->wordspace( $opts->{-wordspace} ); | ||
| 232 | my $charspace = $Pen->charspace( $opts->{-charspace} ); | ||
| 233 | my $hspace = $Pen->hspace( $opts->{-hspace} ); | ||
| 234 | |||
| 235 | $Pen->wordspace( 0 ) if( exists( $opts->{-wordspace} )); | ||
| 236 | $Pen->charspace( 0 ) if( exists( $opts->{-charspace} )); | ||
| 237 | $Pen->hspace( 100 ) if( exists( $opts->{-hspace} )); | ||
| 238 | |||
| 239 | if( $opts->{-print} ){ | ||
| 240 | $Pen->wordspace( $opts->{-wordspace} ) if( exists( $opts->{-wordspace} ) ); | ||
| 241 | $Pen->charspace( $opts->{-charspace} ) if( exists( $opts->{-charspace} ) ); | ||
| 242 | $Pen->hspace( $opts->{-hspace} ) if( exists( $opts->{-hspace} ) ); | ||
| 243 | |||
| 244 | my %text_opts = (); | ||
| 245 | %text_opts = %{ $opts->{text_opts} } if exists( $opts->{text_opts} ); | ||
| 246 | |||
| 247 | $Pen->translate( $opts->{-x}, $opts->{-y} ); | ||
| 248 | $length = $Pen->text( $text, %text_opts ); | ||
| 249 | |||
| 250 | }else{ | ||
| 251 | $length = $Pen->advancewidth( $text ); | ||
| 252 | } | ||
| 253 | |||
| 254 | $Pen->wordspace( $wordspace ) if( exists( $opts->{-wordspace} )); | ||
| 255 | $Pen->charspace( $charspace ) if( exists( $opts->{-charspace} )); | ||
| 256 | $Pen->hspace( $hspace ) if( exists( $opts->{-hspace} )); | ||
| 257 | |||
| 258 | return $length; | ||
| 259 | } | ||
| 260 | |||
| 261 | # -------------------------------------- | ||
| 262 | # Main | ||
| 263 | |||
| 264 | get_cmd_opts(); | ||
| 265 | |||
| 266 | setup(); | ||
| 267 | |||
| 268 | if( @ARGV ){ | ||
| 269 | $Phrase = join( $SPACE, @ARGV ); | ||
| 270 | } | ||
| 271 | my $Paragraph = [ | ||
| 272 | $Phrase, | ||
| 273 | [{ bold=>1 }, $Phrase, ], | ||
| 274 | [{ italic=>1 }, $Phrase, ], | ||
| 275 | [{ bold=>1, italic=>1 }, $Phrase, ], | ||
| 276 | [{ -space_after=>0, text_opts=>{ -underline=>'auto' }, }, | ||
| 277 | $Phrase, $SPACE, | ||
| 278 | [{ bold=>1 }, $Phrase, $SPACE, ], | ||
| 279 | [{ italic=>1 }, $Phrase, $SPACE, ], | ||
| 280 | [{ bold=>1, italic=>1 }, $Phrase, ], | ||
| 281 | ], | ||
| 282 | $SPACE, | ||
| 283 | [{ -space_after=>0 }, | ||
| 284 | small_caps( $P_opts{-size}, 0.65, $Phrase, $SPACE ), | ||
| 285 | [{ bold=>1 }, small_caps( $P_opts{-size}, 0.65, $Phrase, $SPACE ), ], | ||
| 286 | [{ italic=>1 }, small_caps( $P_opts{-size}, 0.65, $Phrase, $SPACE ), ], | ||
| 287 | [{ bold=>1, italic=>1 }, small_caps( $P_opts{-size}, 0.65, $Phrase, ), ], | ||
| 288 | ], | ||
| 289 | $SPACE, | ||
| 290 | $Phrase, | ||
| 291 | ]; | ||
| 292 | print_paragraph( \%P_opts, $Paragraph ); | ||
| 293 | $Pdf->saveas( basename( $0 ) . 'pdf' ); | ||
| 294 | |||
| 295 | __DATA__ |