You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/pitfall/document.rkt

156 lines
5.3 KiB
Racket

6 years ago
#lang debug racket/base
(require
"param.rkt"
"struct.rkt"
racket/class
racket/format
6 years ago
racket/generator
6 years ago
racket/match
racket/list
sugar/unstable/class
sugar/unstable/js
sugar/unstable/dict
"reference.rkt"
"object.rkt"
"page.rkt"
"vector.rkt"
"color.rkt"
"fonts.rkt"
"text.rkt"
"images.rkt"
"annotations.rkt")
7 years ago
(provide PDFDocument)
7 years ago
(define mixed% (annotation-mixin (image-mixin (text-mixin (fonts-mixin (color-mixin (vector-mixin object%)))))))
7 years ago
6 years ago
(define-subclass mixed% (PDFDocument [options (mhash)])
(field [@pageBuffer null]
[@offsets (mhasheqv)] ; The PDF object store
6 years ago
[ref-gen (generator ()
(let loop ([refid 1])
6 years ago
(hash-set! @offsets refid 'missing-ref-offset)
6 years ago
(yield refid)
(loop (add1 refid))))]
6 years ago
[(@root _root) (ref (mhasheq 'Type "Catalog"
'Pages (ref (mhasheq 'Type "Pages"
'Count 0
'Kids empty))))] ; top object
[(@page page) #f] ; The current page
[(@x x) 0]
[(@y y) 0]
[(@info info) (mhasheq
'Producer "PITKIT"
'Creator "PITKIT"
'CreationDate (seconds->date (if (test-mode)
0
(current-seconds)) #f))]) ; Initialize the metadata
7 years ago
7 years ago
;; Initialize mixins
(· this initColor)
(· this initVector)
(· this initFonts)
6 years ago
(inherit-field _fontFamilies)
7 years ago
(· this initText)
(· this initImages)
7 years ago
6 years ago
;; initialize params
(current-compress-streams? (hash-ref options 'compress #t))
(current-auto-first-page (hash-ref options 'autoFirstPage #t))
(current-doc-offset 0)
6 years ago
(define/public (ref [payload (mhash)])
(make-object PDFReference this (ref-gen) payload))
6 years ago
(define/public (write x)
6 years ago
(define bstr (if (bytes? x) x (string->bytes/latin-1 (string-append x "\n"))))
6 years ago
(write-bytes bstr)
6 years ago
(current-doc-offset (file-position (current-output-port))))
6 years ago
(define/public (addPage [options-arg options])
;; end the current page if needed
(unless (hash-ref options 'bufferPages #f)
(flushPages))
;; create a page object
6 years ago
(set! @page (make-object PDFPage this options-arg))
(set! @pageBuffer (cons @page @pageBuffer))
7 years ago
6 years ago
;; in Kids, store page dictionaries in correct order
;; this determines order in document
6 years ago
(define pages (· @root payload Pages payload))
(hash-update! pages 'Kids (λ (val) (append val (list (· @page dictionary)))))
6 years ago
(hash-set! pages 'Count (length (hash-ref pages 'Kids)))
;; reset x and y coordinates
6 years ago
(set! @x (· @page margins left))
(set! @y (· @page margins top))
6 years ago
;; flip PDF coordinate system so that the origin is in
;; the top left rather than the bottom left
(set-field! _ctm this default-ctm-value)
6 years ago
(send this transform 1 0 0 -1 0 (· @page height))
6 years ago
this)
(define/public (flushPages)
6 years ago
(for-each (λ (p) (· p end)) @pageBuffer)
(set! @pageBuffer empty))
6 years ago
(define/public (addContent data)
6 years ago
(send @page write data)
6 years ago
this)
6 years ago
(define/public (_refEnd aref)
6 years ago
(hash-set! @offsets (· aref id) (· aref offset)))
6 years ago
(define/public (end) ; called from source file to finish doc
6 years ago
;; Write the header
(write (format "%PDF-~a" (current-pdf-version))) ; PDF version
(write (string-append "%" (list->string (map integer->char (make-list 4 #xFF))))) ; 4 binary chars, as recommended by the spec
6 years ago
(flushPages)
(define _info (ref))
6 years ago
(for ([(key val) (in-hash @info)])
6 years ago
;; upgrade string literal to String struct
(hash-set! (· _info payload) key (if (string? val) (String val) val)))
(· _info end)
6 years ago
6 years ago
(for ([font (in-hash-values _fontFamilies)])
(· font finalize))
6 years ago
(· @root end)
(· @root payload Pages end)
7 years ago
6 years ago
(define xref-offset (current-doc-offset))
6 years ago
(match-define (list this-idxs this-offsets)
6 years ago
(match (sort (hash->list @offsets) < #:key car) ; sort by refid
6 years ago
[(list (cons idxs offsets) ...) (list idxs offsets)]))
6 years ago
(write "xref")
(write (format "0 ~a" (add1 (length this-offsets))))
(write "0000000000 65535 f ")
(let ([missing-offsets (for/list ([offset (in-list this-offsets)]
[idx (in-list this-idxs)]
#:unless (number? offset))
6 years ago
idx)])
(unless (empty? missing-offsets)
(raise-argument-error 'document:end "numerical offsets" missing-offsets)))
(for ([offset (in-list this-offsets)]
[idx (in-list this-idxs)])
6 years ago
(write (string-append (~r offset #:min-width 10 #:pad-string "0") " 00000 n ")))
6 years ago
(write "trailer")
(write (convert
(mhash 'Size (add1 (length this-offsets))
6 years ago
'Root @root
6 years ago
'Info _info)))
(write "startxref")
(write (number xref-offset))
6 years ago
(write "%%EOF"))
6 years ago
; if no 'info key, nothing will be copied from (hash)
(for ([(key val) (in-hash (hash-ref options 'info (hash)))])
6 years ago
(hash-set! @info key val))
6 years ago
;; Add the first page
(when (current-auto-first-page) (addPage)))
(module+ test
(define d (new PDFDocument)))