structify glyph position

main
Matthew Butterick 6 years ago
parent 8bb5b6d1c9
commit 018edcbe34

@ -54,15 +54,15 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
(list string size (and features (sort features symbol<?)))
(λ ()
(define run (send (· this font) layout string features))
(define width (advanceWidth run))
(define width (glyphrun-advance-width run))
(define scale (/ size (+ (· this font unitsPerEm) 0.0)))
(* width scale))))
;; called from text.rkt
(define/contract (encode this text [features #f])
((string?) ((option/c list?)) . ->*m .
(list/c (listof string?) (listof GlyphPosition?)))
(define (encode this text [features #f])
#;((string?) ((option/c list?)) . ->*m .
(list/c (listof string?) (listof glyphposition?)))
#;(report*/file 'starting-layout-in-embedded (description (· this font)))
(define glyphRun (send (· this font) layout text features))
(define glyphs (glyphrun-glyphs glyphRun))
@ -77,12 +77,12 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
[posn (in-list positions)])
(define gid (send (· this subset) includeGlyph (· glyph id)))
(define subset-idx (toHex gid))
(set-field! advanceWidth posn (· glyph advanceWidth))
(set-glyph-position-advance-width! posn (· glyph advanceWidth))
(hash-ref! (· this widths) gid (λ () (· posn advanceWidth)))
(hash-ref! (· this widths) gid (λ () (glyph-position-advance-width posn)))
(hash-ref! (· this unicode) gid (λ () (· glyph codePoints)))
(send posn scale (· this scale))
(scale-glyph-position! posn (· this scale))
(values subset-idx posn)))
(list subset-idxs new-positions))

@ -36,8 +36,8 @@
(· this dictionary end))
(define/contract (encode this text [options #f])
((string?) ((or/c hash? #f)) . ->*m . (list/c (listof string?) (listof (is-a?/c GlyphPosition))))
(define (encode this text [options #f])
#;((string?) ((or/c hash? #f)) . ->*m . (list/c (listof string?) (listof glyph-position?)))
(define this-font (· this font))
(define encoded (send this-font encodeText text))
(define glyphs (send this-font glyphsForString text))
@ -45,7 +45,7 @@
(define positions
(for/list ([glyph (in-list glyphs)]
[advance (in-list advances)])
(make-object GlyphPosition advance 0 0 0 (send this-font widthOfGlyph glyph))))
(+glyph-position advance 0 0 0 (send this-font widthOfGlyph glyph))))
(list encoded positions))

@ -10,7 +10,8 @@
sugar/unstable/js
sugar/unstable/dict
sugar/list
racket/promise)
racket/promise
fontland/glyph-position)
(provide text-mixin)
#|
@ -221,7 +222,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(when (< last cur)
(let* ([hex (string-append* (sublist encoded-char-strs last cur))]
[posn (list-ref positions (sub1 cur))]
[advance (- (· posn xAdvance) (· posn advanceWidth))])
[advance (- (glyph-position-x-advance posn) (glyph-position-advance-width posn))])
(push-end! commands (format "<~a> ~a" hex (number (- advance))))))
(set! last cur))
@ -240,13 +241,13 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(cond
;; If we have an x or y offset, we have to break out of the current TJ command
;; so we can move the text position.
[(or (not (zero? (· posn xOffset))) (not (zero? (· posn yOffset))))
[(or (not (zero? (glyph-position-x-offset posn))) (not (zero? (glyph-position-y-offset posn))))
;; Flush the current buffer
(flush i)
;; Move the text position and flush just the current character
(send this addContent (format "1 0 0 1 ~a ~a Tm"
(number (+ x (* (· posn xOffset) scale)))
(number (+ y (* (· posn yOffset) scale)))))
(number (+ x (* (glyph-position-x-offset posn) scale)))
(number (+ y (* (glyph-position-y-offset posn) scale)))))
(flush (add1 i))
#t]
[else
@ -255,12 +256,12 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/mixins/text.coffee
(send this addContent (format "1 0 0 1 ~a ~a Tm" (number x) (number y))))
;; Group segments that don't have any advance adjustments
(unless (zero? (- (· posn xAdvance) (· posn advanceWidth)))
(unless (zero? (- (glyph-position-x-advance posn) (glyph-position-advance-width posn)))
(addSegment (add1 i)))
#f]))
(values havingOffset (+ x (* (· posn xAdvance) scale))))
(values havingOffset (+ x (* (glyph-position-x-advance posn) scale))))
;; Flush any remaining commands

Loading…
Cancel
Save