From ebb57d309478dd0e3ca33c68466d3d11702d5803 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 31 May 2017 10:56:21 -0700 Subject: [PATCH] resume in font file embed --- pitfall/pdfkit/lib/font.coffee | 1 - pitfall/pdfkit/lib/mixins/text.coffee | 2 + pitfall/pitfall/document.rkt | 13 ++- pitfall/pitfall/embedded.rkt | 19 ++-- pitfall/pitfall/glyph-position.rkt | 2 +- pitfall/pitfall/test/test12.pdf | Bin 6345 -> 6345 bytes pitfall/pitfall/test/test12rkt.pdf | 150 ++++++++++++++++++++++++++ 7 files changed, 175 insertions(+), 12 deletions(-) diff --git a/pitfall/pdfkit/lib/font.coffee b/pitfall/pdfkit/lib/font.coffee index bf2b6203..c8c58c3a 100644 --- a/pitfall/pdfkit/lib/font.coffee +++ b/pitfall/pdfkit/lib/font.coffee @@ -6,7 +6,6 @@ class PDFFont if StandardFont.isStandardFont src return new StandardFont document, src, id - console.log("openSync on " + src) font = fontkit.openSync src, family else if Buffer.isBuffer(src) diff --git a/pitfall/pdfkit/lib/mixins/text.coffee b/pitfall/pdfkit/lib/mixins/text.coffee index 9a59c7a2..70ca5616 100644 --- a/pitfall/pdfkit/lib/mixins/text.coffee +++ b/pitfall/pdfkit/lib/mixins/text.coffee @@ -253,6 +253,7 @@ module.exports = # If we have a word spacing value, we need to encode each word separately # since the normal Tw operator only works on character code 32, which isn't # used for embedded fonts. + #console.log("wordSpacing="+wordSpacing) if wordSpacing words = text.trim().split(/\s+/) wordSpacing += @widthOfString(' ') + characterSpacing @@ -295,6 +296,7 @@ module.exports = for pos, i in positions # 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. + console.log("pos.xOffset or pos.yOffset=" + (pos.xOffset or pos.yOffset)) if pos.xOffset or pos.yOffset # Flush the current buffer flush i diff --git a/pitfall/pitfall/document.rkt b/pitfall/pitfall/document.rkt index 3e39a2a2..a51253f9 100644 --- a/pitfall/pitfall/document.rkt +++ b/pitfall/pitfall/document.rkt @@ -157,18 +157,23 @@ #;(report* (· this _offsets)) (· this _root end) - (report* (· this _offsets)) + #;(report* (· this _offsets)) (· this _root payload Pages end) - (report* (· this _offsets)) + #;(report* (· this _offsets)) ;; generate xref (define xref-offset (· this _offset)) (with-method ([this-write (this write)]) - (define this-offsets (map cdr (sort (hash->list (· this _offsets)) < #:key car))) ; sort by refid + (define sorted-offset-records (sort (hash->list (· this _offsets)) < #:key car)) ; sort by refid + (define this-offsets (map cdr sorted-offset-records)) + (define this-idxs (map car sorted-offset-records)) (this-write "xref") (this-write (format "0 ~a" (add1 (length this-offsets)))) (this-write "0000000000 65535 f ") - (for ([offset (in-list this-offsets)]) + (for ([offset (in-list this-offsets)] + [idx (in-list this-idxs)]) + (unless (number? offset) + (raise-argument-error 'document:end (format "numerical offset for ref ~a" idx) offset)) (this-write @string-append{@(~r offset #:min-width 10 #:pad-string "0") 00000 n })) (this-write "trailer") ;; trailer (this-write (convert diff --git a/pitfall/pitfall/embedded.rkt b/pitfall/pitfall/embedded.rkt index 2d94849b..df85d4f6 100644 --- a/pitfall/pitfall/embedded.rkt +++ b/pitfall/pitfall/embedded.rkt @@ -70,6 +70,7 @@ For now, we'll just measure width of the characters. ;; todo ;; (send (send (· this subset) encodeStream) pipe fontFile) + (send fontFile end) ;; temp ;; todo ;; (define familyClass (send (· this font) has-table? #"OS/2")) @@ -79,6 +80,7 @@ For now, we'll just measure width of the characters. (define flags 0) ;; generate a random tag (6 uppercase letters. 65 is the char code for 'A') + (when (test-mode) (random-seed 0)) (define tag (list->string (for/list ([i (in-range 6)]) (integer->char (random 65 (+ 65 26)))))) (define name (string-append tag "+" (· this font postscriptName))) @@ -99,6 +101,11 @@ For now, we'll just measure width of the characters. 'XHeight (* (or (· this font xHeight) 0) (· this scale)) 'StemV 0))) + + (hash-set! (· descriptor payload) (if isCFF + 'FontFile3 + 'FontFile2) fontFile) + (· descriptor end) (report (· descriptor toString) 'descriptor-id) @@ -113,11 +120,11 @@ For now, we'll just measure width of the characters. 'Ordering (String "Identity") 'Supplement 0) 'FontDescriptor descriptor - 'W (cons 0 (for/list ([idx (in-range (length (hash-keys (· this widths))))]) + 'W (list 0 (for/list ([idx (in-range (length (hash-keys (· this widths))))]) (hash-ref (· this widths) idx (λ () (error 'embed (format "hash key ~a not found" idx))))))))) (· descendantFont end) - (report (· descendantFont toString) 'descendantFont-id) + (report (· descendantFont toString) 'descendantFont) (hash-set*! (· this dictionary payload) 'Type "Font" 'Subtype "Type0" @@ -142,13 +149,13 @@ For now, we'll just measure width of the characters. (format "<~a>" (string-join encoded " ")))) (send cmap end @string-append{ - /CIDInit /ProcSet findresource begin + /CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << - /Registry (Adobe) - /Ordering (UCS) - /Supplement 0 + /Registry (Adobe) + /Ordering (UCS) + /Supplement 0 >> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def diff --git a/pitfall/pitfall/glyph-position.rkt b/pitfall/pitfall/glyph-position.rkt index 04b81bf8..12d0d32c 100644 --- a/pitfall/pitfall/glyph-position.rkt +++ b/pitfall/pitfall/glyph-position.rkt @@ -25,6 +25,6 @@ (set-field! xAdvance this (* factor (· this xAdvance))) (set-field! yAdvance this (* factor (· this yAdvance))) (set-field! xOffset this (* factor (· this xOffset))) - (set-field! yOffset this (* factor (· this xAdvance))) + (set-field! yOffset this (* factor (· this yOffset))) (set-field! advanceWidth this (* factor (· this advanceWidth))) this) diff --git a/pitfall/pitfall/test/test12.pdf b/pitfall/pitfall/test/test12.pdf index ece1200a43958f072750ad8caf1ca219075aa6b6..0abd17f6ea747353d0bfeaf148ef9d1a3a737fd4 100644 GIT binary patch delta 51 xcmX?Uc+zl#922{tp}C2PvC(8zCM#A~=WrkY$tg@%A*{`I%OhdLALI8r34(R{@ delta 51 xcmX?Uc+zl#922{Nk+G4XvE^h{CM(t;&xokd$tg@%A*{`I%OhdLALI8^X4_^QP diff --git a/pitfall/pitfall/test/test12rkt.pdf b/pitfall/pitfall/test/test12rkt.pdf index e69de29b..6eaa4c82 100644 --- a/pitfall/pitfall/test/test12rkt.pdf +++ b/pitfall/pitfall/test/test12rkt.pdf @@ -0,0 +1,150 @@ +%PDF-1.3 +%ÿÿÿÿ +5 0 obj +<< +/Parent 1 0 R +/Resources 4 0 R +/Contents 3 0 R +/MediaBox [0 0 612 792] +/Type /Page +>> +endobj +4 0 obj +<< +/Font << +/F2 6 0 R +>> +/ProcSet [/PDF /Text /ImageB /ImageC /ImageI] +>> +endobj +3 0 obj +<< +/Length 211 +>> +stream +1 0 0 -1 0 792 cm +q +1 0 0 -1 0 792 cm +BT +1 0 0 1 100 667.5 Tm +/F2 25 Tf +[<0001000200030004000500060004000700060005000800090006000a0005000b000c000500040003000d0004000e000e0004000e0005000f0002000c0006> 0] TJ +ET +Q + +endstream +endobj +7 0 obj +<< +/CreationDate (D:19700101000000Z) +/Creator (PITKIT) +/Producer (PITKIT) +>> +endobj +8 0 obj +<< +>> +endobj +9 0 obj +<< +/StemV 0 +/XHeight 481 +/FontName /WRFKXL+Charter +/Ascent 980 +/CapHeight 671 +/Type /FontDescriptor +/Descent -238 +/Flags 0 +/ItalicAngle 0 +/FontBBox [-161 -236 1193 963] +/FontFile2 8 0 R +>> +endobj +10 0 obj +<< +/BaseFont /WRFKXL+Charter +/CIDSystemInfo << +/Supplement 0 +/Registry (Adobe) +/Ordering (Identity) +>> +/W [0 [278 556 539 843 491 278 334 503 771 280 564 507 568 539 565 321]] +/Subtype /CIDFontType2 +/FontDescriptor 9 0 R +/Type /Font +>> +endobj +11 0 obj +<< +/Length 459 +>> +stream +/CIDInit /ProcSet findresource begin +12 dict begin +begincmap +/CIDSystemInfo << + /Registry (Adobe) + /Ordering (UCS) + /Supplement 0 +>> def +/CMapName /Adobe-Identity-UCS def +/CMapType 2 def +1 begincodespacerange +<0000> +endcodespacerange +1 beginbfrange +<0000> <000f> [<0000> <0053> <006f> <006d> <0065> <0020> <0074> <0078> <0077> <0069> <0068> <0061> <006e> <0062> <0064> <0066>] +endbfrange +endcmap +CMapName currentdict /CMap defineresource pop +end +end + +endstream +endobj +6 0 obj +<< +/ToUnicode 11 0 R +/BaseFont /WRFKXL+Charter +/Encoding /Identity-H +/Subtype /Type0 +/DescendantFonts [10 0 R] +/Type /Font +>> +endobj +2 0 obj +<< +/Pages 1 0 R +/Type /Catalog +>> +endobj +1 0 obj +<< +/Kids [5 0 R] +/Count 1 +/Type /Pages +>> +endobj +xref +0 12 +0000000000 65535 f +0000001740 00000 n +0000001691 00000 n +0000000208 00000 n +0000000119 00000 n +0000000015 00000 n +0000001550 00000 n +0000000470 00000 n +0000000562 00000 n +0000000583 00000 n +0000000787 00000 n +0000001039 00000 n +trailer +<< +/Info 7 0 R +/Root 2 0 R +/Size 12 +>> +startxref +1797 +%%EOF