structify glyph

main
Matthew Butterick 6 years ago
parent 018edcbe34
commit 4b458b7e76

@ -30,7 +30,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
;; we make `unicode` and `width` fields integer-keyed hashes not lists ;; we make `unicode` and `width` fields integer-keyed hashes not lists
;; because they offer better random access and growability ;; because they offer better random access and growability
[unicode (mhash 0 '(0))] ; always include the missing glyph (gid = 0) [unicode (mhash 0 '(0))] ; always include the missing glyph (gid = 0)
[widths (mhash 0 (send (send (· this font) getGlyph 0) advanceWidth))] [widths (mhash 0 (glyph-advance-width (send (· this font) getGlyph 0)))]
;; always include the width of the missing glyph (gid = 0) ;; always include the width of the missing glyph (gid = 0)
[name (· font postscriptName)] [name (· font postscriptName)]
@ -66,21 +66,19 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
#;(report*/file 'starting-layout-in-embedded (description (· this font))) #;(report*/file 'starting-layout-in-embedded (description (· this font)))
(define glyphRun (send (· this font) layout text features)) (define glyphRun (send (· this font) layout text features))
(define glyphs (glyphrun-glyphs glyphRun)) (define glyphs (glyphrun-glyphs glyphRun))
(for ([g (in-list glyphs)])
(· g id))
(define positions (glyphrun-positions glyphRun)) (define positions (glyphrun-positions glyphRun))
#;(report/file (for/list ([p (in-list positions)]) #;(report/file (for/list ([p (in-list positions)])
(list (· p xAdvance) (· p xOffset)))) (list (· p xAdvance) (· p xOffset))))
(define-values (subset-idxs new-positions) (define-values (subset-idxs new-positions)
(for/lists (idxs posns) (for/lists (idxs posns)
([(glyph i) (in-indexed glyphs)] ([(g i) (in-indexed glyphs)]
[posn (in-list positions)]) [posn (in-list positions)])
(define gid (send (· this subset) includeGlyph (· glyph id))) (define gid (send (· this subset) includeGlyph (glyph-id g)))
(define subset-idx (toHex gid)) (define subset-idx (toHex gid))
(set-glyph-position-advance-width! posn (· glyph advanceWidth)) (set-glyph-position-advance-width! posn (glyph-advance-width g))
(hash-ref! (· this widths) gid (λ () (glyph-position-advance-width posn))) (hash-ref! (· this widths) gid (λ () (glyph-position-advance-width posn)))
(hash-ref! (· this unicode) gid (λ () (· glyph codePoints))) (hash-ref! (· this unicode) gid (λ () (glyph-codePoints g)))
(scale-glyph-position! posn (· this scale)) (scale-glyph-position! posn (· this scale))
(values subset-idx posn))) (values subset-idx posn)))
@ -236,6 +234,6 @@ HERE
(check-equal? (bbox->list (· ef bbox)) '(-161 -236 1193 963)) (check-equal? (bbox->list (· ef bbox)) '(-161 -236 1193 963))
(define H-gid 41) (define H-gid 41)
(check-equal? (· ef widths) (mhash 0 278)) (check-equal? (· ef widths) (mhash 0 278))
(check-equal? (send (send (· ef font) getGlyph H-gid) advanceWidth) 738) (check-equal? (glyph-advance-width (send (· ef font) getGlyph H-gid)) 738)
) )
Loading…
Cancel
Save