| 1 |
% |
| 2 |
% \iffalse |
| 3 |
%<*driver> |
| 4 |
\documentclass{tclldoc} |
| 5 |
\PageIndex\CodelineNumbered |
| 6 |
\IndexPrologue{% |
| 7 |
\section*{Index}% |
| 8 |
All numbers in this index are page numbers. |
| 9 |
Underlined entries refer to places where the item in |
| 10 |
question is defined.% |
| 11 |
} |
| 12 |
\setcounter{IndexColumns}{2} |
| 13 |
\begin{document} |
| 14 |
\DocInput{pdf.dtx} |
| 15 |
\end{document} |
| 16 |
%</driver> |
| 17 |
% \fi |
| 18 |
% |
| 19 |
% \title{A simple PDF writer in \Tcllogo} |
| 20 |
% \author{Lars Hellstr\"om} |
| 21 |
% \maketitle |
| 22 |
% |
| 23 |
% \begin{abstract} |
| 24 |
% This file contains some basic routines that allow a \Tcllogo\ |
| 25 |
% script to write PDF files. |
| 26 |
% \end{abstract} |
| 27 |
% |
| 28 |
% \tableofcontents |
| 29 |
% |
| 30 |
% \section{PDF files and objects} |
| 31 |
% |
| 32 |
% A Portable Document Format (PDF) file is, when compared with for example |
| 33 |
% a PostScript file or HTML file, a rather disorganised document. This |
| 34 |
% is because at the basic level, a PDF file is a heap rather than a |
| 35 |
% text; it \emph{can} be ``disorganised'' since its logical structure |
| 36 |
% is based on cross-referencing rather than on sequentiality. The first |
| 37 |
% step is therefore to provide support for writing well-formed heaps. |
| 38 |
% |
| 39 |
% \changes{0.0}{2003/01/02}{Initial version. (LH)} |
| 40 |
% |
| 41 |
% \begin{tcl} |
| 42 |
%<*pkg> |
| 43 |
package require Tcl 8.1 |
| 44 |
package provide writepdf 0.1 |
| 45 |
namespace eval pdf {} |
| 46 |
% \end{tcl} |
| 47 |
% \setnamespace{pdf} |
| 48 |
% |
| 49 |
% |
| 50 |
% \subsection{Building objects} |
| 51 |
% |
| 52 |
% The independent units in a PDF file are called objects. An |
| 53 |
% \emph{object} is essentially a value (which includes a type). The |
| 54 |
% procedures below construct strings of PDF code that encode objects of |
| 55 |
% various types. The strings returned are generally such that one must |
| 56 |
% insert whitespace between two such strings if the data is to be |
| 57 |
% properly encoded. The strings may contain newlines if some building |
| 58 |
% routine thinks the lines should otherwise be too long. |
| 59 |
% |
| 60 |
% \begin{proc}{boolean_obj} |
| 61 |
% The |boolean_obj| procedure returns a boolean object, corresponding |
| 62 |
% to the string passed as its only argument. Most of it is about |
| 63 |
% parsing this string: \texttt{true}, \texttt{yes}, \texttt{on}, and |
| 64 |
% nonzero numbers are interpreted as boolean true, whereas |
| 65 |
% \texttt{false}, \texttt{no}, \texttt{off}, and the zero number is |
| 66 |
% interpreted as boolean false. The matching is case-insensitive. |
| 67 |
% \begin{tcl} |
| 68 |
proc pdf::boolean_obj {value} { |
| 69 |
switch -nocase -- $value { |
| 70 |
true - yes - on {return true} |
| 71 |
false - no - off {return false} |
| 72 |
default { |
| 73 |
if {$value} then {return true} else {return false} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
% \end{tcl} |
| 78 |
% \end{proc} |
| 79 |
% |
| 80 |
% \begin{proc}{int_obj} |
| 81 |
% The |int_obj| procedure returns the PDF object corresponding to |
| 82 |
% the integer supplied as argument. |
| 83 |
% \begin{tcl} |
| 84 |
proc pdf::int_obj {value} {format %d $value} |
| 85 |
% \end{tcl} |
| 86 |
% \end{proc} |
| 87 |
% |
| 88 |
% \begin{proc}{real_obj} |
| 89 |
% \begin{variable}{precision} |
| 90 |
% The |real_obj| procedure returns the PDF object corresponding to |
| 91 |
% the real number supplied as argument. The syntax is |
| 92 |
% \begin{quote} |
| 93 |
% |pdf::real_obj| \word{value} \word{precision}\regopt |
| 94 |
% \end{quote} |
| 95 |
% where \word{precision} is the number of decimals that will be |
| 96 |
% included in the object. If omitted, the value of the |precision| |
| 97 |
% variable is used, and that defaults to $3$. |
| 98 |
% \begin{tcl} |
| 99 |
set pdf::precision 3 |
| 100 |
proc pdf::real_obj {value {precision -1}} { |
| 101 |
if {$precision<0} then { |
| 102 |
unset precision |
| 103 |
variable precision |
| 104 |
} |
| 105 |
format %.[format %d $precision]f $value |
| 106 |
} |
| 107 |
% \end{tcl} |
| 108 |
% \end{variable}\end{proc} |
| 109 |
% |
| 110 |
% \begin{proc}{string_obj} |
| 111 |
% The |string_obj| procedure returns the PDF string object |
| 112 |
% corresponding to the argument. Backslashes and parentheses are |
| 113 |
% escaped. Control characters are converted to escape sequences. |
| 114 |
% Characters with character code above 255 cause an error to be |
| 115 |
% thrown (PDF strings correspond more to \Tcllogo\ byte arrays than to |
| 116 |
% general strings). If the string produced is longer than 100 |
| 117 |
% characters, then backslash--newline sequences are inserted in |
| 118 |
% suitable places. |
| 119 |
% \begin{tcl} |
| 120 |
proc pdf::string_obj {str} { |
| 121 |
if {[regexp "\[^\n\r -\xff\]" $str]} then { |
| 122 |
set estr [string map [list \\ \\\\ ( \\( ) \\) \r \\r \n \\n] $str] |
| 123 |
set str "(" |
| 124 |
set len 1 |
| 125 |
foreach ch [split $estr {}] { |
| 126 |
scan $ch %c code |
| 127 |
if {$code==92} then { |
| 128 |
append str \\ |
| 129 |
incr len |
| 130 |
continue |
| 131 |
} elseif {$code<32} then { |
| 132 |
append str "\\[format %03o $code]" |
| 133 |
incr len 4 |
| 134 |
} elseif {$code<256} then { |
| 135 |
append str $ch |
| 136 |
incr len |
| 137 |
} else { |
| 138 |
error "Bad character $ch [format (U+%04x) $code]\ |
| 139 |
in PDF string." |
| 140 |
} |
| 141 |
if {$len > 100} then { |
| 142 |
append str \\\n |
| 143 |
set len 0 |
| 144 |
} |
| 145 |
} |
| 146 |
append str ")" |
| 147 |
} else { |
| 148 |
set L [list] |
| 149 |
while {[string length $str]>=100} { |
| 150 |
lappend L [string map [list \\ \\\\ ( \\( ) \\) \r \\r \n \\n]\ |
| 151 |
[string range $str 0 99]] |
| 152 |
set str [string range $str 100 end] |
| 153 |
} |
| 154 |
if {[string length $str]} then { |
| 155 |
lappend L\ |
| 156 |
[string map [list \\ \\\\ ( \\( ) \\) \r \\r \n \\n] $str] |
| 157 |
} |
| 158 |
set str ([join $L \\\n]) |
| 159 |
} |
| 160 |
return $str |
| 161 |
} |
| 162 |
% \end{tcl} |
| 163 |
% \end{proc} |
| 164 |
% |
| 165 |
% \begin{proc}{hexstring_obj} |
| 166 |
% The |hexstring_obj| procedure returns the PDF string object, |
| 167 |
% encoded as hexadecimal digits, that corresponds to the argument. |
| 168 |
% If the string is longer than 31 characters then it will be broken |
| 169 |
% on several lines. |
| 170 |
% \begin{tcl} |
| 171 |
proc pdf::hexstring_obj {str} { |
| 172 |
set hstr "<" |
| 173 |
set len 1 |
| 174 |
foreach ch [split $str {}] { |
| 175 |
scan $ch %c code |
| 176 |
if {$len>=63} then { |
| 177 |
append hstr \n |
| 178 |
set len 0 |
| 179 |
} |
| 180 |
if {$code<256} then { |
| 181 |
append hstr [format %02x $code] |
| 182 |
incr len 2 |
| 183 |
} else { |
| 184 |
error "Bad character $ch [format (U+%04x) $code]\ |
| 185 |
in PDF string." |
| 186 |
} |
| 187 |
} |
| 188 |
append hstr ">" |
| 189 |
} |
| 190 |
% \end{tcl} |
| 191 |
% \end{proc} |
| 192 |
% |
| 193 |
% \begin{proc}{text_obj} |
| 194 |
% The |text_obj| procedure returns the PDF string object, encoded as |
| 195 |
% hexadecimal digits, that corresponds to the argument string. This is |
| 196 |
% meant to be used for \emph{text strings} |
| 197 |
% (see~\cite[Ssec.~4.4.1]{PDFspec}), which are \Tcllogo-style strings |
| 198 |
% rather than byte arrays. Text strings may contain general Unicode |
| 199 |
% characters, although they will then be encoded using UTF-16BE. |
| 200 |
% |
| 201 |
% The implementation constructs the 8-bit and 16-bits encodings of the |
| 202 |
% string in parallel, and only when it is done will it decide which to |
| 203 |
% return. |
| 204 |
% \begin{tcl} |
| 205 |
proc pdf::text_obj {str} { |
| 206 |
set hstr "<" |
| 207 |
set ustr "<FEFF" |
| 208 |
set lenh 1 |
| 209 |
set lenu 5 |
| 210 |
set eight_ok 1 |
| 211 |
foreach {chh chl} [split [encoding convertto unicode $str] {}] { |
| 212 |
scan $chh %c codeh |
| 213 |
scan $chl %c codel |
| 214 |
if {$lenu>=62} then { |
| 215 |
append ustr \n |
| 216 |
set lenu 0 |
| 217 |
} |
| 218 |
append ustr [format %02x%02x $codeh $codel] |
| 219 |
incr lenu 4 |
| 220 |
if {$codeh>0} then {set eight_ok 0} |
| 221 |
if {!$eight_ok} then {continue} |
| 222 |
if {$lenh>=63} then { |
| 223 |
append hstr \n |
| 224 |
set lenh 0 |
| 225 |
} |
| 226 |
append hstr [format %02x $codel] |
| 227 |
incr lenh 2 |
| 228 |
} |
| 229 |
append hstr ">" |
| 230 |
append ustr ">" |
| 231 |
if {$eight_ok && ![string match {<feff*} $hstr]} then { |
| 232 |
return $hstr |
| 233 |
} else { |
| 234 |
return $ustr |
| 235 |
} |
| 236 |
} |
| 237 |
% \end{tcl} |
| 238 |
% \end{proc} |
| 239 |
% |
| 240 |
% \begin{proc}{name_obj} |
| 241 |
% The |name_obj| procedure returns the PDF name object corresponding |
| 242 |
% to its argument. It is useful mainly for names with strange |
| 243 |
% characters in them (such as spaces), but most names (e.g.\ dictionary |
| 244 |
% keys) appearing in PDF files do not require any quoting and can |
| 245 |
% therefore just as well be written as explicit PDF code. |
| 246 |
% \begin{tcl} |
| 247 |
proc pdf::name_obj {str} { |
| 248 |
if {[string length $str]>126} then { |
| 249 |
error "String too long to be a PDF name." |
| 250 |
} |
| 251 |
set res / |
| 252 |
foreach ch [split $str {}] { |
| 253 |
switch -glob -- $ch { |
| 254 |
( - ) - < - > - \\[ - \\] - \{ - \} - / - % - # { |
| 255 |
scan $ch %c code |
| 256 |
append res [format #%02x $code] |
| 257 |
} |
| 258 |
[!-~] {append res $ch} |
| 259 |
default { |
| 260 |
scan $ch %c code |
| 261 |
append res [format #%02x $code] |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return $res |
| 266 |
} |
| 267 |
% \end{tcl} |
| 268 |
% \end{proc} |
| 269 |
% |
| 270 |
% \begin{proc}{array_obj} |
| 271 |
% The |array_obj| procedure builds an array object of the objects it |
| 272 |
% is given as arguments. The syntax is |
| 273 |
% \begin{quote} |
| 274 |
% |pdf::array_obj| \word{object}\regstar |
| 275 |
% \end{quote} |
| 276 |
% Newlines are inserted between the objects if it does not appear as |
| 277 |
% if the object would fit on a single (100 character) line. |
| 278 |
% \begin{tcl} |
| 279 |
proc pdf::array_obj {args} { |
| 280 |
set res \[ |
| 281 |
set len 1 |
| 282 |
foreach item $args { |
| 283 |
if {[string length $item] + $len >= 100} then { |
| 284 |
append res \n |
| 285 |
set len 0 |
| 286 |
} elseif {[string length $res]>1} then { |
| 287 |
append res " " |
| 288 |
incr len |
| 289 |
} |
| 290 |
append res $item |
| 291 |
incr len [string length $item] |
| 292 |
} |
| 293 |
if {$len >= 100} then { |
| 294 |
append res \n |
| 295 |
} |
| 296 |
append res \] |
| 297 |
} |
| 298 |
% \end{tcl} |
| 299 |
% \end{proc} |
| 300 |
% |
| 301 |
% \begin{proc}{dict_obj} |
| 302 |
% The |dict_obj| procedure builds a dictionary object from its |
| 303 |
% arguments. The syntax is |
| 304 |
% \begin{quote} |
| 305 |
% |pdf::dict_obj| |
| 306 |
% \begin{regexp}[\regstar]\word{key} \word{value}\end{regexp} |
| 307 |
% \end{quote} |
| 308 |
% where each \word{key} must be a name object and each \word{value} |
| 309 |
% must be an object. It is checked that the number of elements is |
| 310 |
% correct and that the keys begin with a slash. |
| 311 |
% \begin{tcl} |
| 312 |
proc pdf::dict_obj {args} { |
| 313 |
if {[llength $args] % 2 != 0} then { |
| 314 |
error "Not the same number of keys and values." |
| 315 |
} |
| 316 |
set res "<<\n" |
| 317 |
foreach {key value} $args { |
| 318 |
if {![string match {/*} $key]} then { |
| 319 |
error "'$key' isn't a name object." |
| 320 |
} |
| 321 |
if {[string length $key] + [string length $value]>99} then { |
| 322 |
append res $key \n $value \n |
| 323 |
} else { |
| 324 |
append res $key { } $value \n |
| 325 |
} |
| 326 |
} |
| 327 |
append res ">>" |
| 328 |
} |
| 329 |
% \end{tcl} |
| 330 |
% \end{proc} |
| 331 |
% |
| 332 |
% \begin{proc}{null_obj} |
| 333 |
% The |null_obj| procedure returns a null object. It has no arguments. |
| 334 |
% \begin{tcl} |
| 335 |
proc pdf::null_obj {} {return null} |
| 336 |
% \end{tcl} |
| 337 |
% \end{proc} |
| 338 |
% |
| 339 |
% |
| 340 |
% Objects can also be \emph{streams}, but those have a special relation |
| 341 |
% to the file structure and are therefore best treated in conjunction |
| 342 |
% with that. In particular, streams cannot be used as arguments of |
| 343 |
% |array_obj| or |dict_obj|. The arguments of these procedures can |
| 344 |
% however be \emph{indirect references} to objects of any type, but |
| 345 |
% these too are best treated in the context of the basic PDF file |
| 346 |
% structure. |
| 347 |
% |
| 348 |
% |
| 349 |
% \subsection{File structure} |
| 350 |
% |
| 351 |
% The body of a PDF file consists of a sequence of \emph{indirect |
| 352 |
% objects}, which are mainly a sort of declarations: a pair of integers |
| 353 |
% are associated with an object value. Since any composite object can |
| 354 |
% (and in several cases must) contain a reference to any indirect object, |
| 355 |
% this makes it possible to build up arbitrary data structures. It is |
| 356 |
% however also a complication, since it requires that there is a |
| 357 |
% mechanism for allocating these numbers. |
| 358 |
% |
| 359 |
% \begin{arrayvar}{file\meta{fnum}} |
| 360 |
% Every file that \Tcllogo\ opens gets a unique identifier which is |
| 361 |
% used in calls to |puts| and such. This identifier is also used as |
| 362 |
% the name of an array in the |pdf| namespace, in which the |
| 363 |
% procedures below store all auxiliary information they need to create |
| 364 |
% a proper PDF file. |
| 365 |
% \end{arrayvar} |
| 366 |
% |
| 367 |
% \begin{arrayentry}{file\meta{fnum}}{!\meta{reference label}} |
| 368 |
% \begin{arrayentry}{file\meta{fnum}}{last_object_num} |
| 369 |
% In this API, references to indirect objects can be arbitrary |
| 370 |
% strings, called \emph{reference labels}. The correspondence to the |
| 371 |
% object numbers actually found in the file is given by the |
| 372 |
% \texttt{!}\meta{reference label} entries in the array of the file in |
| 373 |
% question. The entries in this array are lists with the structure |
| 374 |
% \begin{quote} |
| 375 |
% \word{object number} \word{generation number} \word{file |
| 376 |
% position}\regopt |
| 377 |
% \end{quote} |
| 378 |
% where the \word{file position} is present only if the indirect |
| 379 |
% object in question has been written to file already. The |
| 380 |
% \word{object number} is the number of the object referred to. The |
| 381 |
% \word{generation number} is currently always zero; it appears that |
| 382 |
% it can only be nonzero for files that have incrementally updated, |
| 383 |
% and this API only supports creating a file from scratch. The |
| 384 |
% \word{file position} is the position in the file of the beginning |
| 385 |
% of the indirect object begin referred to. |
| 386 |
% |
| 387 |
% The |last_object_num| entry in the array holds the most recently |
| 388 |
% allocated object number. It is incremented whenever a new reference |
| 389 |
% label is encountered. |
| 390 |
% \end{arrayentry}\end{arrayentry} |
| 391 |
% |
| 392 |
% \begin{proc}{obj_ref} |
| 393 |
% The |obj_ref| procedure returns PDF code for an indirect reference |
| 394 |
% to an object. The syntax is |
| 395 |
% \begin{quote} |
| 396 |
% |pdf::obj_ref| \word{file} \word{reference label} |
| 397 |
% \end{quote} |
| 398 |
% where \word{file} is the indentifier of the PDF file in question. |
| 399 |
% If the \word{reference label} has not been encountered before for |
| 400 |
% this particular file, then a new object number is allocated for it. |
| 401 |
% \begin{tcl} |
| 402 |
proc pdf::obj_ref {F label} { |
| 403 |
upvar #0 [namespace current]::$F A |
| 404 |
if {![info exists A(!$label)]} then { |
| 405 |
incr A(last_object_num) |
| 406 |
set A(!$label) [list $A(last_object_num) 0] |
| 407 |
} |
| 408 |
format {%d %d R} [lindex $A(!$label) 0] [lindex $A(!$label) 1] |
| 409 |
} |
| 410 |
% \end{tcl} |
| 411 |
% \end{proc} |
| 412 |
% |
| 413 |
% \begin{proc}{begin_stream} |
| 414 |
% \begin{proc}{end_stream} |
| 415 |
% The |begin_stream| and |end_stream| procedures delimit the creation |
| 416 |
% of a \emph{stream object}. Between two such commands, it is possible |
| 417 |
% to write arbitrary text (usually page descriptors or some sort of |
| 418 |
% embedded data) to the PDF file and have it inserted correctly into |
| 419 |
% the file as the data stored in the stream object. |
| 420 |
% |
| 421 |
% The syntax for |begin_stream| is |
| 422 |
% \begin{quote} |
| 423 |
% |pdf::begin_stream| \word{file} \word{reference label} |
| 424 |
% \begin{regexp}[\regstar]\word{key} \word{value}\end{regexp} |
| 425 |
% \end{quote} |
| 426 |
% where \word{file} of course is the file to write to and |
| 427 |
% \word{reference label} is the string that should be used to |
| 428 |
% reference this object. Each stream consists of one dictionary part |
| 429 |
% and one data part, where the primary task of the dictionary part is |
| 430 |
% to specify how the data part should be interpreted. The most |
| 431 |
% important element in the dictionary is the \texttt{/Length} key and |
| 432 |
% its value---these are inserted by the |begin_stream| and |
| 433 |
% |end_stream| commands, so one needs not worry about those---but if |
| 434 |
% for example the data part is encoded in some special way (for |
| 435 |
% example, it might be compressed) then it is necessary to include |
| 436 |
% additional elements in the dictionary. This is what the \word{key} |
| 437 |
% and \word{value} arguments are for. |
| 438 |
% |
| 439 |
% \begin{arrayentry}{file\meta{fnum}}{current_stream} |
| 440 |
% The |current_stream| entry in a PDF file array is set if and only |
| 441 |
% if the current position in that file is inside a stream. It is |
| 442 |
% not possible to begin a new stream when this entry is set. The |
| 443 |
% value of this entry is a list with the structure |
| 444 |
% \begin{quote} |
| 445 |
% \word{reference label} \word{start} |
| 446 |
% \end{quote} |
| 447 |
% where \word{reference label} is the reference label of the stream |
| 448 |
% and \word{start} is the position in the file of the first byte in |
| 449 |
% the stream data. Both of these are needed at |end_stream| to |
| 450 |
% record the length of the stream data. |
| 451 |
% \end{arrayentry} |
| 452 |
% |
| 453 |
% \begin{arrayentry}{file\meta{fnum}}{?\meta{reference label}} |
| 454 |
% This kind of entry is used for indirect objects that are lengths |
| 455 |
% of the stream whose reference label is the \meta{reference label}. |
| 456 |
% They have the same syntax as their |!| ordinary counterparts. |
| 457 |
% \end{arrayentry} |
| 458 |
% |
| 459 |
% \begin{tcl} |
| 460 |
proc pdf::begin_stream {F label args} { |
| 461 |
upvar #0 [namespace current]::$F A |
| 462 |
if {[info exists A(current_stream)]} then { |
| 463 |
error "There is already a stream ([lindex $A(current_stream) 0])\ |
| 464 |
being written to in this file." |
| 465 |
} |
| 466 |
if {![info exists A(!$label)]} then { |
| 467 |
incr A(last_object_num) |
| 468 |
set A(!$label) [list $A(last_object_num) 0] |
| 469 |
} |
| 470 |
set A(?$label) [list [incr A(last_object_num)] 0] |
| 471 |
lappend A(!$label) [tell $F] |
| 472 |
puts $F\ |
| 473 |
[format {%d %d obj} [lindex $A(!$label) 0] [lindex $A(!$label) 1]] |
| 474 |
puts $F [eval\ |
| 475 |
[list dict_obj /Length [format {%d 0 R} $A(last_object_num)]]\ |
| 476 |
$args] |
| 477 |
puts $F stream |
| 478 |
set A(current_stream) [list $label [tell $F]] |
| 479 |
} |
| 480 |
% \end{tcl} |
| 481 |
% |
| 482 |
% The |end_stream| procedure takes the target file as its only argument. |
| 483 |
% It finishes off the stream as necessary. It also evaluates |
| 484 |
% everything that has been placed in the backlog of the file. |
| 485 |
% |
| 486 |
% \begin{arrayentry}{file\meta{fnum}}{backlog} |
| 487 |
% It is not possible to output a new indirect object when a stream |
| 488 |
% is being written to, but it can still be at such a time that the |
| 489 |
% need for such an object is discovered. The |backlog| entry |
| 490 |
% provides a way around that limitation---this entry is a script |
| 491 |
% that is evaluated (and cleared) at the end of every |end_stream|, |
| 492 |
% hence commands can be delayed by appending them to this script, |
| 493 |
% instead of evaluating them immediately. |
| 494 |
% |
| 495 |
% New commands are appended to the |backlog|, and must be preceeded |
| 496 |
% by a command separator. |
| 497 |
% \end{arrayentry} |
| 498 |
% |
| 499 |
% \begin{tcl} |
| 500 |
proc pdf::end_stream {F} { |
| 501 |
upvar #0 [namespace current]::$F A |
| 502 |
if {![info exists A(current_stream)]} then { |
| 503 |
error "There is no stream to end." |
| 504 |
} |
| 505 |
set length [expr {[tell $F] - [lindex $A(current_stream) 1]}] |
| 506 |
set label [lindex $A(current_stream) 0] |
| 507 |
unset A(current_stream) |
| 508 |
puts $F "endstream endobj" |
| 509 |
lappend A(?$label) [tell $F] |
| 510 |
puts $F [format {%d %d obj %d endobj} [lindex $A(?$label) 0]\ |
| 511 |
[lindex $A(?$label) 1] $length] |
| 512 |
eval "set A(backlog) {}; $A(backlog)" |
| 513 |
} |
| 514 |
% \end{tcl} |
| 515 |
% \end{proc}\end{proc} |
| 516 |
% |
| 517 |
% \begin{proc}{put_obj} |
| 518 |
% The |put_obj| procedure writes a direct object to a PDF file. The |
| 519 |
% syntax is |
| 520 |
% \begin{quote} |
| 521 |
% |pdf::put_obj| \word{file} \word{reference label} \word{object} |
| 522 |
% \end{quote} |
| 523 |
% \begin{tcl} |
| 524 |
proc pdf::put_obj {F label obj} { |
| 525 |
upvar #0 [namespace current]::$F A |
| 526 |
if {[info exists A(current_stream)]} then { |
| 527 |
append A(backlog) \n [list put_obj $F $label $obj] |
| 528 |
return |
| 529 |
} |
| 530 |
if {![info exists A(!$label)]} then { |
| 531 |
incr A(last_object_num) |
| 532 |
set A(!$label) [list $A(last_object_num) 0] |
| 533 |
} |
| 534 |
lappend A(!$label) [tell $F] |
| 535 |
puts $F\ |
| 536 |
[format {%d %d obj} [lindex $A(!$label) 0] [lindex $A(!$label) 1]] |
| 537 |
puts $F $obj |
| 538 |
puts $F endobj |
| 539 |
} |
| 540 |
% \end{tcl} |
| 541 |
% \end{proc} |
| 542 |
% |
| 543 |
% \begin{proc}{rewrite_pdf} |
| 544 |
% The |rewrite_pdf| procedure opens a new PDF file for writing and |
| 545 |
% initialises the associated data structures. The syntax is |
| 546 |
% \begin{quote} |
| 547 |
% |pdf::rewrite_pdf| \word{file name} \meta{options} |
| 548 |
% \end{quote} |
| 549 |
% and the return value is the identifier of the file opened. The |
| 550 |
% \word{file name} is of course the name of that file. The |
| 551 |
% \meta{options} is zero or more of |
| 552 |
% \begin{quote} |
| 553 |
% |-permissions| \word{integer}\\ |
| 554 |
% |-header| \word{string} |
| 555 |
% \end{quote} |
| 556 |
% The permissions are the default permissions for the file in question. |
| 557 |
% If this is not specified, then no such value is specified to |open|, |
| 558 |
% The header is a string that will be put first in the file (as header). |
| 559 |
% It defaults to |
| 560 |
% \begin{quote} |
| 561 |
% \texttt{\%PDF-1.3}\\ |
| 562 |
% \texttt{\%\r{a}\"a\"o} |
| 563 |
% \end{quote} |
| 564 |
% where the first line is a standard header line, and the second line |
| 565 |
% is there to help some software understand that the file should be |
| 566 |
% treated as a binary file. \textbf{Note} that no newline is inserted |
| 567 |
% after this string; be sure to include it in the string if necessary. |
| 568 |
% \begin{tcl} |
| 569 |
proc pdf::rewrite_pdf {name args} { |
| 570 |
set Opt(-header) %PDF-1.3\n%\xe5\xe4\xf6\n |
| 571 |
array set Opt $args |
| 572 |
if {[info exists Opt(-permissions)]} then { |
| 573 |
set F [open $name w $Opt(-permissions)] |
| 574 |
} else { |
| 575 |
set F [open $name w] |
| 576 |
} |
| 577 |
fconfigure $F -translation binary |
| 578 |
puts -nonewline $F $Opt(-header) |
| 579 |
upvar #0 [namespace current]::$F A |
| 580 |
array unset A |
| 581 |
set A(last_object_num) 0 |
| 582 |
set A(backlog) "" |
| 583 |
return $F |
| 584 |
} |
| 585 |
% \end{tcl} |
| 586 |
% \end{proc} |
| 587 |
% |
| 588 |
% |
| 589 |
% \begin{proc}{close_pdf} |
| 590 |
% The |close_pdf| procedure performs the non-trivial task of finishing |
| 591 |
% off the PDF file and closing it. The syntax is |
| 592 |
% \begin{quote} |
| 593 |
% |pdf::close_pdf| \word{file} \word{catalog label} |
| 594 |
% \begin{regexp}[\regstar]\word{key} \word{value}\end{regexp} |
| 595 |
% \end{quote} |
| 596 |
% and the return value is a report detailing any problems |
| 597 |
% encountered (such as objects that are referred to but never |
| 598 |
% defined). This is a report rather than an error, because there is |
| 599 |
% in many cases no sharp distinction. If the return value is |
| 600 |
% non-empty, then there is probably a bug in your program that needs |
| 601 |
% to be fixed. |
| 602 |
% |
| 603 |
% The \word{file} is the identifier of the file to write. The |
| 604 |
% \word{catalog label} is the reference label of the Catalog object |
| 605 |
% in the document. The remaining arguments can be used to insert |
| 606 |
% additional information (such as a reference to the Info dictionary |
| 607 |
% of the document) in the trailer dictionary. |
| 608 |
% |
| 609 |
% \begin{tcl} |
| 610 |
proc pdf::close_pdf {F label args} { |
| 611 |
upvar #0 [namespace current]::$F A |
| 612 |
set reportL [list] |
| 613 |
% \end{tcl} |
| 614 |
% The first step is to compile the cross-reference table of the |
| 615 |
% document. I originally made one subsection for each range of defined |
| 616 |
% indirect objects, giving the mandatory free entry \#0 a separate |
| 617 |
% subsection, but for some reason Adobe software didn't like that at |
| 618 |
% all.\footnote{Whether this means Adobe isn't following their own |
| 619 |
% standard I leave to others to decide. Neither GhostScript nor |
| 620 |
% Quartz (the PDF-based graphics system in Mac~OS~X) seemed to have |
| 621 |
% any problems with this arrangement.} Hence the current |
| 622 |
% implementation is to make a cross-reference table with only one |
| 623 |
% subsection, with an explicit free entry for every missing item. |
| 624 |
% |
| 625 |
% \changes{0.1}{2003/02/24}{Changed cross-reference section to avoid |
| 626 |
% what is probably a bug in Adobe PDF readers. (LH)} |
| 627 |
% |
| 628 |
% The |xrA| array constructed below is a prototype for the |
| 629 |
% cross-reference section. It is indexed by object number and the |
| 630 |
% entries have the list structure |
| 631 |
% \begin{quote} |
| 632 |
% \word{file position} \word{generation number} \word{type} |
| 633 |
% \end{quote} |
| 634 |
% Just as in a PDF file, the \word{type} is either \texttt{f} or |
| 635 |
% \texttt{n} depending on whether the entry is ``free'' or |
| 636 |
% ``in use''. The \word{file position} and \word{generation number} |
| 637 |
% are however not padded with zeros, and the \word{file position} is |
| 638 |
% initially an empty string in the ``free'' entries. |
| 639 |
% |
| 640 |
% This first round simply collects the information and detects |
| 641 |
% collisions. |
| 642 |
% \begin{tcl} |
| 643 |
set xrA(0) [list "" 65535 f] |
| 644 |
foreach lbl [array names A {[!?]*}] { |
| 645 |
set idx [lindex $A($lbl) 0] |
| 646 |
set ent [list [lindex $A($lbl) 2] [lindex $A($lbl) 1] n] |
| 647 |
if {[llength $A($lbl)]<3} then { |
| 648 |
lappend reportL "There is no indirect object with label:\ |
| 649 |
[string range $lbl 1 end]" |
| 650 |
set ent [replace $ent 2 2 f] |
| 651 |
} elseif {[llength $A($lbl)]>3} then { |
| 652 |
lappend reportL "Multiple indirect objects for label\ |
| 653 |
[string range $lbl 1 end]; at\ |
| 654 |
[join [lrange $A($lbl) 2 end]]." |
| 655 |
} |
| 656 |
if {![info exists xrA($idx)]} then { |
| 657 |
set xrA($idx) $ent |
| 658 |
} elseif {[lindex $xrA($idx) 2]=="f" && [lindex $ent 2]=="n"}\ |
| 659 |
then { |
| 660 |
lappend reportL "This shouldn't happen:\ |
| 661 |
There are several reference labels for\ |
| 662 |
indirect object $idx. Using that with label:\ |
| 663 |
[string range $lbl 1 end]" |
| 664 |
set xrA($idx) $ent |
| 665 |
} else { |
| 666 |
lappend reportL "This shouldn't happen:\ |
| 667 |
There are several reference labels for\ |
| 668 |
indirect object $idx. Ignoring that with label:\ |
| 669 |
[string range $lbl 1 end]" |
| 670 |
} |
| 671 |
} |
| 672 |
% \end{tcl} |
| 673 |
% The second round makes sure that there is a contiguous sequence of |
| 674 |
% reference numbers and constructs the linked list of free entries. |
| 675 |
% \begin{tcl} |
| 676 |
set last_free 0 |
| 677 |
set maxidx [lindex [lsort -integer -decreasing [array names xrA]] 0] |
| 678 |
for {set n $maxidx} {$n>=0} {incr n -1} { |
| 679 |
if {![info exists xrA($n)]} then { |
| 680 |
set xrA($n) [list "" 0 f] |
| 681 |
lappend reportL "This shouldn't happen:\ |
| 682 |
Object number $n was allocated, but not\ |
| 683 |
assigned a reference label." |
| 684 |
} |
| 685 |
if {[lindex $xrA($n) 2]=="f"} then { |
| 686 |
set xrA($n) [lreplace $xrA($n) 0 0 $last_free] |
| 687 |
set last_free $n |
| 688 |
} |
| 689 |
} |
| 690 |
% \end{tcl} |
| 691 |
% Now the cross-reference section can be written to file. |
| 692 |
% \begin{tcl} |
| 693 |
set startxref [tell $F] |
| 694 |
puts $F xref |
| 695 |
puts $F [format {%d %d} 0 [expr {$maxidx + 1}]] |
| 696 |
for {set n 0} {$n<=$maxidx} {incr n} { |
| 697 |
puts $F [eval [list format {%010d %05d %1s }] $xrA($n)] |
| 698 |
} |
| 699 |
% \end{tcl} |
| 700 |
% Having completed the cross-reference table, the second step is |
| 701 |
% to write the trailer. |
| 702 |
% \begin{tcl} |
| 703 |
puts $F trailer |
| 704 |
if {[info exists A(!$label)]} then { |
| 705 |
lappend args /Root [format {%d %d R} [lindex $A(!$label) 0]\ |
| 706 |
[lindex $A(!$label) 1]] |
| 707 |
} else { |
| 708 |
lappend reportL "ERROR: The document does not have a catalog."\ |
| 709 |
"There is no indirect object with label: $label" |
| 710 |
} |
| 711 |
lappend args /Size [array size xrA] |
| 712 |
puts $F [eval [list dict_obj] $args] |
| 713 |
puts $F "startxref\n${startxref}\n%%EOF" |
| 714 |
% \end{tcl} |
| 715 |
% The final step is to close the file and compile the report. |
| 716 |
% \begin{tcl} |
| 717 |
close $F |
| 718 |
join $reportL \n |
| 719 |
} |
| 720 |
%</pkg> |
| 721 |
% \end{tcl} |
| 722 |
% \end{proc} |
| 723 |
% |
| 724 |
% |
| 725 |
% \subsection{Hello World} |
| 726 |
% |
| 727 |
% The code below creates a PDF file matching the basic ``Hello World'' |
| 728 |
% example \cite[Sec.~A.2]{PDFspec}. |
| 729 |
% |
| 730 |
% \begin{tcl} |
| 731 |
%<*example1> |
| 732 |
set F [pdf::rewrite_pdf hello.pdf] |
| 733 |
pdf::put_obj $F "The catalog" [pdf::dict_obj\ |
| 734 |
% |
| 735 |
/Type /Catalog\ |
| 736 |
% |
| 737 |
/Pages [pdf::obj_ref $F "The pages"]\ |
| 738 |
% |
| 739 |
/Outlines [pdf::obj_ref $F "The outlines"]] |
| 740 |
pdf::put_obj $F "The outlines"\ |
| 741 |
[pdf::dict_obj /Type /Outlines /Count [pdf::int_obj 0]] |
| 742 |
pdf::put_obj $F "The pages" [pdf::dict_obj\ |
| 743 |
% |
| 744 |
/Type /Pages\ |
| 745 |
% |
| 746 |
/Count [pdf::int_obj 1]\ |
| 747 |
% |
| 748 |
/Kids [pdf::array_obj [pdf::obj_ref $F "Page 1"]]] |
| 749 |
pdf::put_obj $F "Page 1" [pdf::dict_obj\ |
| 750 |
% |
| 751 |
/Type /Page\ |
| 752 |
% |
| 753 |
/Parent [pdf::obj_ref $F "The pages"]\ |
| 754 |
% |
| 755 |
/Resources [pdf::dict_obj\ |
| 756 |
% |
| 757 |
/Font [pdf::dict_obj /F1 [pdf::obj_ref $F "Helvetica"]]\ |
| 758 |
% |
| 759 |
/ProcSet [pdf::obj_ref $F "The procs"]]\ |
| 760 |
% |
| 761 |
/MediaBox [pdf::array_obj [pdf::int_obj 0] [pdf::int_obj 0]\ |
| 762 |
[pdf::int_obj 612] [pdf::int_obj 792]]\ |
| 763 |
% |
| 764 |
/Contents [pdf::obj_ref $F "Page 1 contents"]] |
| 765 |
pdf::begin_stream $F "Page 1 contents" |
| 766 |
puts $F {BT} |
| 767 |
puts $F {/F1 24 Tf} |
| 768 |
puts $F {100 100 Td (Hello World) Tj} |
| 769 |
puts $F {ET} |
| 770 |
pdf::end_stream $F |
| 771 |
pdf::put_obj $F "The procs" [pdf::array_obj /PDF /Text] |
| 772 |
pdf::put_obj $F "Helvetica" [pdf::dict_obj\ |
| 773 |
/Type /Font /Subtype /Type1\ |
| 774 |
/Name /F1 /BaseFont /Helvetica /Encoding /MacRomanEncoding] |
| 775 |
pdf::close_pdf $F "The catalog" |
| 776 |
%</example1> |
| 777 |
% \end{tcl} |
| 778 |
% |
| 779 |
% |
| 780 |
% \begin{thebibliography}{9} |
| 781 |
% \bibitem{PDFspec} |
| 782 |
% Adobe Systems Incorporated: |
| 783 |
% \textit{Portable Document Format Reference Manual}, |
| 784 |
% version~1.3 (second edition), Addison--Wesley, 1999; |
| 785 |
% ISBN 0-201-61588-6; |
| 786 |
% \textsc{http:}/\slash \texttt{partners.adobe.com}\slash |
| 787 |
% \texttt{asn}\slash \texttt{developer}\slash \texttt{acrosdk}\slash |
| 788 |
% \texttt{docs}\slash \texttt{filefmtspecs}\slash |
| 789 |
% \texttt{PDFReference13.pdf}. |
| 790 |
% \emph{Note:} There is now a version~1.4 of this specification. |
| 791 |
% \end{thebibliography} |
| 792 |
% |
| 793 |
% \PrintIndex |
| 794 |
% |
| 795 |
% |
| 796 |
\endinput |