stop dragging my doc around

main
Matthew Butterick 5 years ago
parent 46ca51245b
commit 1c30d5aee3

@ -26,8 +26,6 @@
(define current-font (make-parameter #f))
(define current-font-size (make-parameter 12))
(define current-ref-listeners (make-parameter null))
;; helpers
(define (numberizer x #:round [round? #true])

@ -20,8 +20,8 @@
(define PDFDocument
(class (annotation-mixin (image-mixin (text-mixin (fonts-mixin (vector-mixin (color-mixin object%))))))
(set-current-id! 1)
(current-ref-listeners (cons (λ (ref) (send this log-ref ref)) (current-ref-listeners)))
(set-current-ref-id! 1)
(register-ref-listener (λ (ref) (send this log-ref ref)))
(super-new)
(init-field [(@options options) (mhasheq)])
@ -60,7 +60,7 @@
(define/public (add-page [options-arg @options])
;; create a page object
(define page-parent (dict-ref @root 'Pages))
(set! @pages (cons (make-object PDFPage this page-parent options-arg) @pages))
(set! @pages (cons (make-object PDFPage page-parent options-arg) @pages))
;; reset x and y coordinates
(set! @x (margin-left (get-field margins (page))))

@ -34,7 +34,6 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
(define EmbeddedFont
(class PDFFont
(init document)
(init-field font id)
(field [subset (create-subset font)]
;; we make `unicode` and `width` fields integer-keyed hashes not lists
@ -44,16 +43,14 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
;; always include the width of the missing glyph (gid = 0)
[name (font-postscript-name font)]
[scale (/ 1000 (font-units-per-em font))])
(super-new [document document]
[ascender (* (font-ascent font) scale)]
(super-new [ascender (* (font-ascent font) scale)]
[descender (* (font-descent font) scale)]
[bbox (font-bbox font)]
[line-gap (* (font-linegap font) scale)])
(inherit-field [@ascender ascender]
[@descender descender]
[@dictionary dictionary]
[@document document])
[@dictionary dictionary])
(define/override (string-width string size [features #f])
; #f disables features ; null enables default features ; list adds features

@ -1,18 +1,16 @@
#lang racket/base
(require
racket/class
racket/contract
"standard-font.rkt"
"font.rkt"
fontland
"embedded.rkt")
(provide PDFFont-open)
(define/contract (PDFFont-open document src family id)
(object? any/c any/c any/c . -> . (is-a?/c PDFFont))
(define (PDFFont-open src family id)
(cond
[(and (string? src) (isStandardFont src))
(make-object StandardFont document src id)]
(make-object StandardFont src id)]
[else
(define font
(cond
@ -20,4 +18,4 @@
[(path? src) (open-font (path->string src))]
;; todo: other font-loading cases
[else (raise-argument-error 'PDFFont-open "loadable font thingy" src)]))
(make-object EmbeddedFont document font id)]))
(make-object EmbeddedFont font id)]))

@ -5,8 +5,7 @@
(define PDFFont
(class object%
(super-new)
(init-field [(@document document) #f]
[(@ascender ascender) #f]
(init-field [(@ascender ascender) #f]
[(@descender descender) #f]
[(@line-gap line-gap) #f]
[(@bbox bbox) #f])

@ -39,7 +39,7 @@
[_ ; if not, load the font
(set! @font-count (add1 @font-count))
(define id (format "F~a" @font-count))
(set! @current-font (PDFFont-open this src family id))
(set! @current-font (PDFFont-open src family id))
;; check for existing font families with the same name already in the PDF
(match (hash-ref @font-families (get-field name @current-font) #f)
[(? values font) (set! @current-font font)]

@ -38,7 +38,7 @@
[(and (object? src) (· src width) (· src height)) src]
[else (send this openImage src)]))
(unless (· image obj) (send image embed this))
(unless (· image obj) (send image embed))
(hash-ref! (· this page xobjects) (· image label) (· image obj))

@ -43,7 +43,7 @@
(define obj #f)
(JPEG label width height obj data bits channels colorSpace)))
(define (embed this doc-in)
(define (embed this)
#;(object? . ->m . void?)
(unless (· this obj)

@ -51,8 +51,8 @@
[(port? bytes-or-port) bytes-or-port]
[else (current-input-port)])) signed big-endian))
(define/contract (embed this doc-in)
(object? . ->m . void?)
(define/contract (embed this)
(->m void?)
(unless (· this obj)
(set-field! obj this

@ -6,7 +6,7 @@
"reference.rkt"
"core.rkt"
sugar/unstable/js)
(define p (make-object PDFPage (make-object PDFDocument)))
(define p (make-object PDFPage))
(check-equal? (· p size) "letter")
(check-equal? (· p layout) "portrait")
(check-equal? (· p margins) (margin 72 72 72 72))

@ -11,7 +11,7 @@
(define PDFPage
(class object%
(super-new)
(init-field @doc [@page-parent #false] [@options (mhash)])
(init-field [@page-parent #false] [@options (mhash)])
(field [(@size size) (hash-ref @options 'size "letter")]
[(@layout layout) (hash-ref @options 'layout "portrait")]
[@dimensions (if (list? @size)

@ -20,7 +20,6 @@
[width (· image width)]
[height (· image height)]
[imgData (· image imgData)]
[document #f]
[alphaChannel #f]
[obj #f])
@ -28,10 +27,9 @@
embed
split-alpha-channel))
(define/contract (embed this doc-in)
(object? . ->m . void?)
(define/contract (embed this)
(->m void?)
(set-field! document this doc-in)
(unless (· this obj)
(set-field! obj this

@ -7,7 +7,7 @@
"core.rkt"
"object.rkt"
"zlib.rkt")
(provide PDFReference set-current-id! make-ref)
(provide (all-defined-out))
(define dictable<%>
(interface* ()
@ -20,8 +20,12 @@
(define (dict-set! refobj key val) (send refobj set-key! key val))
(define (dict-update! refobj key updater [failure-result (λ () (error 'update-no-key))]) (send refobj update-key! key updater failure-result)))])))
(define ref-listeners null)
(define (register-ref-listener proc)
(set! ref-listeners (cons proc ref-listeners)))
(define current-id 0)
(define (set-current-id! val)
(define (set-current-ref-id! val)
(set! current-id val))
(define (make-ref [payload (make-hasheq)])
@ -37,7 +41,7 @@
(field [(@offset offset) #f]
[@port (open-output-bytes)])
(for-each (λ (proc) (proc this)) (current-ref-listeners))
(for-each (λ (proc) (proc this)) ref-listeners)
(define/public (write x)
(write-bytes (to-bytes x) @port))

@ -12,13 +12,11 @@
(define StandardFont
(class PDFFont
(init document)
(init-field name id)
(field [font (make-object AFMFont
((hash-ref standard-fonts name
(λ () (raise-argument-error 'PDFFont "valid font name" name)))))])
(super-new [document document]
[ascender (get-field ascender font)]
(super-new [ascender (get-field ascender font)]
[descender (get-field descender font)]
[bbox (get-field bbox font)]
[line-gap (get-field line-gap font)])
@ -27,8 +25,7 @@
[@descender descender]
[@line-gap line-gap]
[@bbox bbox]
[@dictionary dictionary]
[@document document])
[@dictionary dictionary])
(define/override (embed)
(set-field! payload @dictionary
@ -56,7 +53,7 @@
(* width scale))))
(module+ test
(define stdfont (make-object StandardFont #f "Helvetica" #f)))
(define stdfont (make-object StandardFont "Helvetica" #f)))
(define (isStandardFont name) (hash-ref standard-fonts name #f))

Loading…
Cancel
Save