diff --git a/pitfall/pitfall/embedded-font.rkt b/pitfall/pitfall/embedded-font.rkt index 70a8991a..5e282b1f 100644 --- a/pitfall/pitfall/embedded-font.rkt +++ b/pitfall/pitfall/embedded-font.rkt @@ -56,35 +56,42 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee [@descender descender] [@ref ref]) - (define/override (string-width string size [features #f]) + (define/override (string-width str size [features null]) ; #f disables features ; null enables default features ; list adds features (hash-ref! width-cache - (list string size (and features (sort features symbolvector subset-idxs) (list->vector new-positions))) + (define layout-cache (make-hash)) + + (define/override (encode str [features null]) + (define layout-subcache + (hash-ref! layout-cache features make-hash)) + + (hash-ref! layout-subcache str + (λ () + (define glyph-run (layout font str features)) + (define glyphs (glyphrun-glyphs glyph-run)) + (define positions (glyphrun-positions glyph-run)) + (define-values (subset-idxs new-positions) + (for/lists (idxs posns) + ([(g i) (in-indexed glyphs)] + [posn (in-list positions)]) + (define gid (subset-add-glyph! subset (glyph-id g))) + (define subset-idx (to-hex gid)) + (set-glyph-position-advance-width! posn (glyph-advance-width g)) + + (hash-ref! widths gid (λ () (glyph-position-advance-width posn))) + (hash-ref! unicode gid (λ () (glyph-codepoints g))) + + (scale-glyph-position! posn scale) + (values subset-idx posn))) + (list (list->vector subset-idxs) (list->vector new-positions))))) (define/override (embed) diff --git a/pitfall/pitfall/text.rkt b/pitfall/pitfall/text.rkt index f8b93b1e..44741293 100644 --- a/pitfall/pitfall/text.rkt +++ b/pitfall/pitfall/text.rkt @@ -1,4 +1,4 @@ -#lang racket/base +#lang debug racket/base (require "core.rkt" "page.rkt"