From bf36013ef3de659f4850ca9f19db521d4049455a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 7 Jul 2017 00:53:29 -0700 Subject: [PATCH] mutational propaganda --- pitfall/fontkit/default-shaper.rkt | 2 +- pitfall/fontkit/font.rkt | 2 +- pitfall/fontkit/glyph-iterator.rkt | 1 + pitfall/fontkit/glyphinfo.rkt | 4 +- pitfall/fontkit/gsub-processor.rkt | 11 +- pitfall/fontkit/layout-engine.rkt | 6 +- pitfall/fontkit/ot-layout-engine.rkt | 4 +- pitfall/fontkit/ot-processor.rkt | 45 +++-- pitfall/fontkit/shaping-plan.rkt | 13 +- pitfall/pdfkit/node_modules/fontkit/index.js | 24 +++ pitfall/pitfall/embedded.rkt | 2 +- pitfall/pitfall/test/out.bin | Bin 4268 -> 3994 bytes pitfall/pitfall/test/out.tx | 201 +++++++++++++++++++ pitfall/pitfall/test/out.txt | 201 +++++++++++++++++++ pitfall/pitfall/test/test16.coffee | 2 +- pitfall/pitfall/test/test16.pdf | Bin 6184 -> 5890 bytes pitfall/pitfall/test/test16.rkt | 2 +- 17 files changed, 484 insertions(+), 36 deletions(-) create mode 100644 pitfall/pitfall/test/out.tx create mode 100644 pitfall/pitfall/test/out.txt diff --git a/pitfall/fontkit/default-shaper.rkt b/pitfall/fontkit/default-shaper.rkt index e32f4aff..8c3f8211 100644 --- a/pitfall/fontkit/default-shaper.rkt +++ b/pitfall/fontkit/default-shaper.rkt @@ -15,7 +15,7 @@ (define-subclass object% (DefaultShaper) (define/public (plan plan_ glyphs features) - (report*/file plan_ glyphs features) + #;(report*/file plan_ glyphs features) ;; Plan the features we want to apply (planPreprocessing plan_) (planFeatures plan_) diff --git a/pitfall/fontkit/font.rkt b/pitfall/fontkit/font.rkt index 11e267b3..7249edfc 100644 --- a/pitfall/fontkit/font.rkt +++ b/pitfall/fontkit/font.rkt @@ -236,7 +236,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js ((string?) ((option/c (listof symbol?)) (option/c symbol?) (option/c symbol?)) . ->*m . GlyphRun?) (unless (· this _layoutEngine) (set-field! _layoutEngine this (+LayoutEngine this))) - (report*/file 'in-layout (· this _layoutEngine)) + #;(report*/file 'in-layout (· this _layoutEngine)) (send (· this _layoutEngine) layout string userFeatures script language)) diff --git a/pitfall/fontkit/glyph-iterator.rkt b/pitfall/fontkit/glyph-iterator.rkt index f1e1c058..2153e6cb 100644 --- a/pitfall/fontkit/glyph-iterator.rkt +++ b/pitfall/fontkit/glyph-iterator.rkt @@ -67,6 +67,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GlyphIterator.js (check-equal? (send gi move 1) 'b) (check-equal? (send gi move 1) 'c) (check-false (send gi move 1)) + (check-false (send gi cur)) (check-equal? (send gi increment -3) 'a) (check-equal? (send gi cur) 'a) (check-equal? (send gi peek 1) 'b) diff --git a/pitfall/fontkit/glyphinfo.rkt b/pitfall/fontkit/glyphinfo.rkt index 3f5b2379..b2211920 100644 --- a/pitfall/fontkit/glyphinfo.rkt +++ b/pitfall/fontkit/glyphinfo.rkt @@ -32,8 +32,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GlyphInfo.js (define/public (id [id-in #f]) (cond [(not id-in) _id] - [else (set! _id id-in) - (set! substituted #t) + [else (set-field! _id this id-in) + (set-field! substituted this #t) (cond [(and (· this _font GDEF) (· this _font GDEF glyphClassDef)) diff --git a/pitfall/fontkit/gsub-processor.rkt b/pitfall/fontkit/gsub-processor.rkt index e4ef22a1..849ccd07 100644 --- a/pitfall/fontkit/gsub-processor.rkt +++ b/pitfall/fontkit/gsub-processor.rkt @@ -10,7 +10,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GSUBProcessor.js (define-subclass OTProcessor (GSUBProcessor) (define/override (applyLookup lookupType table) - (report* 'GSUBProcessor:applyLookup lookupType) + (report lookupType 'GSUBProcessor:applyLookup) (case lookupType [(1) ;; Single Substitution (report 'single-substitution) @@ -18,7 +18,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GSUBProcessor.js (cond [(= index -1) #f] [else (define glyph (· this glyphIterator cur)) - (set-field! id glyph + (send glyph id (case (· table version) [(1) (bitwise-and (+ (· glyph id) (· table deltaGlyphID)) #xffff)] [(2) (send (· table substitute) get index)])) @@ -29,7 +29,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GSUBProcessor.js (cond [(= index -1) #f] [else (define sequence (send (· table sequences) get index)) - (set-field! id (· this glyphIterator cur) (list-ref sequence 0)) + (send (· this glyphIterator cur) id (list-ref sequence 0)) (set-field! ligatureComponent (· this glyphIterator cur) 0) (define features (· this glyphIterator cur features)) @@ -52,7 +52,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GSUBProcessor.js (cond [(= index -1) #f] [else (define USER_INDEX 0) - (set-field! id (· this glyphIterator cur) (list-ref (send (· table alternateSet) get index) USER_INDEX)) + (send (· this glyphIterator cur) id (list-ref (send (· table alternateSet) get index) USER_INDEX)) #t])] [(4) ;; Ligature substitution @@ -151,8 +151,11 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/GSUBProcessor.js (set-field! ligatureComponent (list-ref (· this glyphs) i) ligatureComponent))) ;; Delete the matched glyphs, and replace the current glyph with the ligature glyph + (report* (for/list ([g (· this glyphs)]) (· g id)) (· this glyphIterator index)) (set-field! glyphs this (drop-right (· this glyphs) (length matched))) (set-field! glyphs this (list-set (· this glyphs) (· this glyphIterator index) ligatureGlyph)) + (set-field! glyphs (· this glyphIterator) (· this glyphs)) + (report* (for/list ([g (· this glyphs)]) (· g id)) (· this glyphIterator index)) #t)] [else #f])]))) diff --git a/pitfall/fontkit/layout-engine.rkt b/pitfall/fontkit/layout-engine.rkt index a85ea383..1c5c36f2 100644 --- a/pitfall/fontkit/layout-engine.rkt +++ b/pitfall/fontkit/layout-engine.rkt @@ -18,7 +18,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/LayoutEngine.js (cond [(· this font has-morx-table?) (error 'morx-layout-unimplemented)] [(or (· this font has-gsub-table?) (· this font has-gpos-table?)) - (report/file 'starting-layout-engine) + #;(report/file 'starting-layout-engine) (+OTLayoutEngine (· this font))] [else #f])]) @@ -43,7 +43,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/LayoutEngine.js (send (· this font) glyphsForString str-or-glyphs) str-or-glyphs)) - (report*/file 'starting-layout-in-layout-engine glyphs) + #;(report*/file 'starting-layout-in-layout-engine glyphs) (cond [(empty? glyphs) (+GlyphRun glyphs empty)] ; Return early if there are no glyphs [else @@ -53,6 +53,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/LayoutEngine.js ;; Substitute and position the glyphs (set! glyphs (send this substitute glyphs features script language)) + (report/file 'end-sub) + (error 'stop) (report/file 'ready-position) (define positions (send this position glyphs features script language)) (report/file 'fired-position) diff --git a/pitfall/fontkit/ot-layout-engine.rkt b/pitfall/fontkit/ot-layout-engine.rkt index 39b44c56..1542680c 100644 --- a/pitfall/fontkit/ot-layout-engine.rkt +++ b/pitfall/fontkit/ot-layout-engine.rkt @@ -13,7 +13,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTLayoutEngine.js [GSUBProcessor #f] [GPOSProcessor #f]) - (report/file 'starting-ot-layout-engine) + #;(report/file 'starting-ot-layout-engine) (when (· font has-gsub-table?) (set-field! GSUBProcessor this (+GSUBProcessor font (or (· font GSUB) (error 'no-gsub-table))))) @@ -31,7 +31,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTLayoutEngine.js ;; This determines which features to apply to which glyphs. (set! shaper (Shapers-choose script)) (set! plan (+ShapingPlan (· this font) script language)) - (report/file shaper) + #;(report/file shaper) (send (make-object shaper) plan (· this plan) (· this glyphInfos) features)) (define/public (substitute glyphs . _) diff --git a/pitfall/fontkit/ot-processor.rkt b/pitfall/fontkit/ot-processor.rkt index 1ca83ded..a068d8a2 100644 --- a/pitfall/fontkit/ot-processor.rkt +++ b/pitfall/fontkit/ot-processor.rkt @@ -74,23 +74,23 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js (define/public (lookupsForFeatures [userFeatures empty] [exclude #f]) - (report*/file 'ot-proc:lookupsForFeatures) + #;(report*/file 'ot-proc:lookupsForFeatures) (sort (for*/list ([tag (in-list userFeatures)] - [feature (in-value (dict-ref (report/file (· this features)) tag #f))] + [feature (in-value (dict-ref (· this features) tag #f))] #:when feature [lookupIndex (in-list (· feature lookupListIndexes))] #:unless (and exclude (index-of exclude lookupIndex))) - (report*/file tag lookupIndex) + #;(report*/file tag lookupIndex) (mhasheq 'feature tag 'index lookupIndex 'lookup (send (· this table lookupList) get lookupIndex))) - < #:key (λ (i) (report*/file (· i index))))) + < #:key (λ (i) (· i index)))) (define/public (applyFeatures userFeatures glyphs advances) - (report/file 'ot-proc:applyFeatures-part1) + #;(report/file 'ot-proc:applyFeatures-part1) (define lookups (send this lookupsForFeatures userFeatures)) - (report/file 'ot-proc:applyFeatures-part2) + #;(report/file 'ot-proc:applyFeatures-part2) (report (length lookups)) (send this applyLookups lookups glyphs advances)) @@ -98,17 +98,33 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js (define/public (applyLookups lookups glyphs positions) (set-field! glyphs this glyphs) (set-field! positions this positions) - (report/file 'ot-proc:applyLookups) + #;(report/file 'ot-proc:applyLookups) + (report (for/list ([g glyphs]) (· g id))) (set-field! glyphIterator this (+GlyphIterator glyphs)) + (for* ([lookup-entry (in-list lookups)]) - (define feature (dict-ref lookup-entry 'feature)) - (define lookup (dict-ref lookup-entry 'lookup)) + (define feature (· lookup-entry feature)) + (define lookup (· lookup-entry lookup)) + (report 'resetting-iterator) (send (· this glyphIterator) reset (· lookup flags)) - (while (< (· this glyphIterator index) (length glyphs)) - (when (dict-has-key? (· this glyphIterator cur features) feature) + + (while (< (or (· this glyphIterator index) 0) (length (· this glyphs))) + (report (length (· this glyphs)) 'glyphs-length-top) + (report (for/list ([g (· this glyphs)]) (· g id)) 'gids-top) + (report (· this glyphIterator index) giterator-idx-top) + (report* feature (· this glyphIterator cur id) (· this glyphIterator cur features)) + (report (dict-has-key? (· this glyphIterator cur features) feature)) + (cond + [(not (dict-has-key? (· this glyphIterator cur features) feature)) + (send (· this glyphIterator) next)] + [else + (report/file '=================) + (report* (for/list ([g (· this glyphs)]) (· g id)) (for/list ([g (· this glyphIterator glyphs)]) (· g id)) (for/list ([g glyphs]) (· g id)) (· this glyphIterator index) (· this glyphIterator cur id) (· this glyphIterator peekIndex)) (for/or ([table (in-list (· lookup subTables))]) - (send this applyLookup (· lookup lookupType) table))) - (send (· this glyphIterator) next)))) + (send this applyLookup (· lookup lookupType) table)) + (report 'incrementing-iterator-at-bottom) + (send (· this glyphIterator) next) + (report* (· this glyphIterator cur) (· this glyphIterator index))])))) (abstract applyLookup) @@ -117,8 +133,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js (error)) (define/public (coverageIndex coverage [glyph #f]) - (unless glyph - (set! glyph (· this glyphIterator cur id))) + (unless glyph (set! glyph (· this glyphIterator cur id))) (or (case (· coverage version) [(1) (index-of (· coverage glyphs) glyph)] [(2) (for/first ([range (in-list (· coverage rangeRecords))] diff --git a/pitfall/fontkit/shaping-plan.rkt b/pitfall/fontkit/shaping-plan.rkt index 23df9ce1..9b87fa14 100644 --- a/pitfall/fontkit/shaping-plan.rkt +++ b/pitfall/fontkit/shaping-plan.rkt @@ -22,7 +22,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/ShapingPlan.js ;; Adds the given features to the last stage. ;; Ignores features that have already been applied. (define/public (_addFeatures features) - (report*/file 'stages-before stages) + #;(report*/file 'stages-before stages) (match-define (list head-stages ... last-stage) stages) (set! stages `(,@head-stages @@ -31,7 +31,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/ShapingPlan.js #:unless (dict-ref (· this allFeatures) feature #f)) (dict-set! (· this allFeatures) feature #t) feature)))) - (report*/file 'stages-after stages)) + #;(report*/file 'stages-after stages) + stages) ;; Adds the given features to the global list (define/public (_addGlobal features) @@ -69,23 +70,23 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/ShapingPlan.js ;; Assigns the global features to the given glyphs (define/public (assignGlobalFeatures glyphs) - (report*/file glyphs (· this globalFeatures)) + #;(report*/file glyphs (· this globalFeatures)) (for* ([glyph (in-list glyphs)] [feature (in-dict-keys (· this globalFeatures))]) (dict-set! (· glyph features) feature #t))) ;; Executes the planned stages using the given OTProcessor (define/public (process processor glyphs [positions #f]) - (report*/file 'shaping-plan-process processor) + #;(report*/file 'shaping-plan-process processor) (send processor selectScript (· this script) (· this language)) - (report/file stages) + #;(report/file stages) (for ([stage (in-list stages)]) (cond [(and (procedure? stage) (not positions)) (stage (· this font) glyphs positions)] [(> (length stage) 0) - (report*/file 'shaping-plan:applying-features processor) + #;(report*/file 'shaping-plan:applying-features processor) (send processor applyFeatures stage glyphs positions)])))) diff --git a/pitfall/pdfkit/node_modules/fontkit/index.js b/pitfall/pdfkit/node_modules/fontkit/index.js index 4e8317bf..8728b584 100644 --- a/pitfall/pdfkit/node_modules/fontkit/index.js +++ b/pitfall/pdfkit/node_modules/fontkit/index.js @@ -7273,6 +7273,7 @@ var OTProcessor = function () { this.glyphs = glyphs; this.positions = positions; this.glyphIterator = new GlyphIterator(glyphs); + console.log("glyph ids =" + glyphs.map(function(o) { return o.id;})) for (var _iterator8 = lookups, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _getIterator(_iterator8);;) { var _ref8; @@ -7290,14 +7291,29 @@ var OTProcessor = function () { feature = _ref9.feature, lookup = _ref9.lookup; + console.log("resetting iterator"); this.glyphIterator.reset(lookup.flags); while (this.glyphIterator.index < glyphs.length) { + console.log("glyphs.length at top = " + glyphs.length); + console.log("glyph ids at top =" + glyphs.map(function(o) { return o.id;})) + console.log("this.glyphIterator.index at top = " + this.glyphIterator.index); + console.log("feature = " + feature); + console.log("this.glyphIterator.cur.id = " + this.glyphIterator.cur.id); + console.log("(feature in this.glyphIterator.cur.features) = " + (feature in this.glyphIterator.cur.features)) if (!(feature in this.glyphIterator.cur.features)) { this.glyphIterator.next(); continue; } + console.log("=================================="); + console.log("glyph ids =" + glyphs.map(function(o) { return o.id;})) + console.log("this.glyph ids =" + this.glyphs.map(function(o) { return o.id;})) + console.log("glyphIterator.glyph ids =" + this.glyphIterator.glyphs.map(function(o) { return o.id;})) + console.log("this.glyphIterator.index = " + this.glyphIterator.index); + console.log("this.glyphIterator.cur.id = " + this.glyphIterator.cur.id); + console.log("this.glyphIterator.peekIndex() = " + this.glyphIterator.peekIndex()); + for (var _iterator9 = lookup.subTables, _isArray9 = Array.isArray(_iterator9), _i9 = 0, _iterator9 = _isArray9 ? _iterator9 : _getIterator(_iterator9);;) { var _ref10; @@ -7318,7 +7334,10 @@ var OTProcessor = function () { } } + console.log("incrementing iterator at bottom"); this.glyphIterator.next(); + console.log("this.glyphIterator.cur = bottom = " + this.glyphIterator.cur); + console.log("this.glyphIterator.index = bottom = " + this.glyphIterator.index); } } }; @@ -8608,11 +8627,16 @@ var GSUBProcessor = function (_OTProcessor) { } // Delete the matched glyphs, and replace the current glyph with the ligature glyph + + console.log("glyph ids before =" + this.glyphs.map(function(o) { return o.id;})) + console.log("this.glyphIterator.index = " + this.glyphIterator.index) for (var _i5 = matched.length - 1; _i5 >= 0; _i5--) { this.glyphs.splice(matched[_i5], 1); } this.glyphs[this.glyphIterator.index] = ligatureGlyph; + console.log("glyph ids after =" + this.glyphs.map(function(o) { return o.id;})) + console.log("this.glyphIterator.index = " + this.glyphIterator.index) return true; } diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index c5928c37..790a711a 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -43,7 +43,7 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee (define/contract (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))) + #;(report*/file 'starting-layout-in-embedded (description (· this font))) (define glyphRun (send (· this font) layout text features)) (define glyphs (· glyphRun glyphs)) (for ([g (in-list glyphs)]) diff --git a/pitfall/pitfall/test/out.bin b/pitfall/pitfall/test/out.bin index c240bdf43de9fc87521f1fea1f94d2688bdfb72a..788641abc384272eeca7f6c9ded023529df9b5f1 100644 GIT binary patch delta 134 zcmZ3ZI7@zlbu3$MVnqQ11pEO~3dv<93J@-%43NH5P?QRlXM6|b?@Z6BOoPZXEoWe0 z49UnXsetgAe*VHE=-kY?J+K` z(JVm2k{EoXy*J1LRjT1o$-)u^Q4X{jq}oA+ft`VoiM^gN8O202b&-({JnG7VKp?ED z$jK$Et;(jXq-H9prlzdKCMs&ot}FAtB+x$`(o?0>Z-ltb&#nOom(>s+vBI|BM+W&N_NK2XJvQa{X-<6JukV#Zb*; O!@$erx0#V;CN}`d_(D # Set the font, draw some text doc.font('the-font') .fontSize(100) - .text('fifl', 100, 100, {width: false}) + .text('fi', 100, 100, {width: false}) doc.end() diff --git a/pitfall/pitfall/test/test16.pdf b/pitfall/pitfall/test/test16.pdf index 068176282fae52dd81339dfc13030a2f6605afcf..1868626ea464300f91456c26080ff64e6d6c0090 100644 GIT binary patch delta 348 zcmZ2s(4;rv38T@(r%{ug88cbUO^pmpCvRl5Wb+Agc5)1!{DJW{jCql%c5)E25|f$P z<}zjpMn=QQ{VbxBH?Z`>6u7e9U@^9|G?}c(<`&DAn^;l600Do1ltOY@i2{VnCr%@~h1_nlx?HMy!jSMV}4JWTqf$StXW z@L4&5G*4PVdM=bNI(Y+IEI%`l7sRlN(SU&w$YS0s#r~729?0hX|Aon%=?#M_!vY6> zd08e#b~Z*PMpZ@*W@az%4I%*%4oVD+jO@`2?CjCrtc=XeOp!nZOflXJ92^YMEI`AO z7<{C?H^>52s^L({!V(2h4zwAh+ChbZoq>^wy`C`{#Y8l9k&zBO>dJyZAgrp$$tA0; z%BHNOW-6$rrmVy!Dr(HGEC__A#-=7_#ztb|rsjeoV#Y@1Oy(+Th9(MTN`_iZoRuLV zA>qNw7D^!k!ovKlf|eFchFlz~nm&&Ij2R`)I(jNjjGczt#RabvEE�chL8XN diff --git a/pitfall/pitfall/test/test16.rkt b/pitfall/pitfall/test/test16.rkt index 477a09c2..51c303f8 100644 --- a/pitfall/pitfall/test/test16.rkt +++ b/pitfall/pitfall/test/test16.rkt @@ -12,7 +12,7 @@ (send* doc [font "the-font"] [fontSize 100] - [text "fifl" 100 100 (hash 'width #f)])) + [text "fi" 100 100 (hash 'width #f)])) (define-runtime-path this "test16rkt.pdf")