main
Matthew Butterick 5 years ago
parent f8205be8ab
commit 9195f4218c

@ -40,8 +40,8 @@
;; initialize mixins
(inherit-field @ctm) ; from vector mixin
(inherit-field @font-families) (inherit font) ; from font mixin
(send this initText)
(send this initImages)
(send this init-text)
(send this init-images)
;; initialize params
(current-compress-streams? (hash-ref @options 'compress #t))

@ -54,7 +54,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
[@dictionary dictionary]
[@document document])
(define/override (widthOfString string size [features #f])
(define/override (string-width string size [features #f])
; #f disables features ; null enables default features ; list adds features
(hash-ref! width-cache
(list string size (and features (sort features symbol<?)))
@ -202,7 +202,7 @@ HERE
(require rackunit fontland sugar/unstable/js)
(define f (open-font "../ptest/assets/charter.ttf"))
(define ef (make-object EmbeddedFont #f f #f))
(check-equal? (send ef widthOfString "f" 1000) 321.0)
(check-equal? (send ef string-width "f" 1000) 321.0)
(check-equal? (· ef ascender) 980)
(check-equal? (· ef descender) -238)
(check-equal? (· ef line-gap) 0)

@ -13,7 +13,7 @@
(field [(@dictionary dictionary) #f]
[@embedded #f])
(abstract embed encode widthOfString)
(abstract embed encode string-width)
(define/public (ref)
(unless @dictionary
@ -25,8 +25,8 @@
(embed)
(set! @embedded #t)))
(define/public (lineHeight size [includeGap #f])
(define gap (if includeGap @line-gap 0))
(define/public (line-height size [include-gap #f])
(define gap (if include-gap @line-gap 0))
(* (/ (+ @ascender gap (- @descender)) 1000.0) size))))

@ -53,7 +53,7 @@
this)
(define/public (current-line-height [include-gap #f])
(send @current-font lineHeight @current-font-size include-gap))
(send @current-font line-height @current-font-size include-gap))
(define/public (register-font name src [family #f])
(hash-set! @registered-fonts name (make-hash (list (cons 'src src)

@ -16,13 +16,13 @@
[_imageCount #f])
(as-methods
initImages
init-images
image
openImage
)))
(define/contract (initImages this)
(define/contract (init-images this)
(->m void?)
(set-field! _imageRegistry this (mhash))
(set-field! _imageCount this 0))

@ -48,7 +48,7 @@
(+glyph-position advance 0 0 0 (send font widthOfGlyph glyph))))
(list encoded positions))
(define/override (widthOfString str size [options #f])
(define/override (string-width str size [options #f])
(define glyphs (send font glyphsForString str))
(define advances (send font advancesForGlyphs glyphs))
(define width (apply + advances))

@ -26,17 +26,17 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
[_textOptions #f])
(as-methods
initText
_initOptions
init-text
init-options
line-gap
moveDown
moveUp
move-down
move-up
_text
_fragment
text
widthOfString)))
string-width)))
(define/contract (initText this)
(define/contract (init-text this)
(->m void?)
(set-field! x this 0)
(set-field! y this 0)
@ -50,21 +50,21 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
this)
(define/contract (moveDown this [lines 1] #:factor [factor 1])
(define/contract (move-down this [lines 1] #:factor [factor 1])
(() (number? #:factor number?) . ->*m . object?)
(increment-field! y this (* factor (send this current-line-height #t) (+ lines (· this _line-gap))))
this)
(define/contract (moveUp this [lines 1])
(define/contract (move-up this [lines 1])
(() (number?) . ->*m . object?)
(moveDown this #:factor -1))
(move-down this #:factor -1))
(define/contract (_text this text x y options lineCallback)
(string? (or/c number? #f) (or/c number? #f) hash? procedure? . ->m . object?)
(let* ([options (send this _initOptions options x y)]
(let* ([options (send this init-options options x y)]
[text (format "~a" text)] ;; Convert text to a string
;; if the wordSpacing option is specified, remove multiple consecutive spaces
[text (if (hash-ref options 'wordSpacing #f)
@ -86,14 +86,14 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(send this _text text-string x y options (λ args (apply _line this args))))
(define/contract (widthOfString this str [options (mhash)])
(define/contract (string-width this str [options (mhash)])
((string?) (hash?) . ->*m . number?)
#;(report str 'measuring-width-of)
(+ (send (· this current-font) widthOfString str (· this current-font-size) (hash-ref options 'features #f))
(+ (send (· this current-font) string-width str (· this current-font-size) (hash-ref options 'features #f))
(* (hash-ref options 'characterSpacing 0) (sub1 (string-length str)))))
(define/contract (_initOptions this [options (mhash)] [x #f] [y #f])
(define/contract (init-options this [options (mhash)] [x #f] [y #f])
(() (hash? (or/c number? #f) (or/c number? #f)) . ->*m . hash?)
;; clone options object
@ -127,7 +127,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
;; 180325 suppress the size tracking: we'll do our own line measurement
;; 181120 unsuppress the size tracking for now because it breaks test 04
(if (not wrapper)
(increment-field! x this (send this widthOfString text))
(increment-field! x this (send this string-width text))
(increment-field! y (+ (send this current-line-height #t) line-gap)))
(void))
@ -144,7 +144,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(define renderedWidth
;; wrap this in delay so it's only calculated if needed
(delay
(+ (or (· options textWidth) (widthOfString this text options))
(+ (or (· options textWidth) (string-width this text options))
(* wordSpacing (sub1 (or (· options wordCount) 0)))
(* characterSpacing (sub1 (string-length text))))))

@ -54,7 +54,7 @@ Mauris at ante tellus. Vestibulum a metus lectus. Praesent tempor purus a lacus
# Draw some text wrapped to 412 points wide
doc.text('And here is some wrapped text...', 100, 300)
.font('Charter', 13)
.moveDown() # move down 1 line
.move-down() # move down 1 line
.text(loremIpsum, width: 412, align: 'justify', indent: 30, paragraphGap: 5)
# Add another page, and set the font back

Loading…
Cancel
Save