|
|
|
@ -28,23 +28,21 @@ https://github.com/mbutterick/pdfkit/blob/master/lib/font/embedded.coffee
|
|
|
|
|
embed
|
|
|
|
|
toUnicodeCmap))
|
|
|
|
|
|
|
|
|
|
(define/contract (widthOfString this str size [features #f])
|
|
|
|
|
((string? number?) ((or/c list? #f)) . ->*m . number?)
|
|
|
|
|
#|
|
|
|
|
|
PDFKit makes a whole layout here and measures that.
|
|
|
|
|
For now, we'll just measure width of the characters.
|
|
|
|
|
|#
|
|
|
|
|
#;(define run (send (· this font) layout string)) ; todo: features would be passed here
|
|
|
|
|
#;(define width (· run advanceWidth))
|
|
|
|
|
#;(define scale (/ size (· this font unitsPerEm)))
|
|
|
|
|
#;(* width scale)
|
|
|
|
|
(send (· this font) measure-string str size))
|
|
|
|
|
(define/contract (widthOfString this string size [features #f])
|
|
|
|
|
((string? number?) ((option/c list?)) . ->*m . number?)
|
|
|
|
|
(cond
|
|
|
|
|
[features
|
|
|
|
|
(define run (send (· this font) layout string features)) ; todo: features would be passed here
|
|
|
|
|
(define width (· run advanceWidth))
|
|
|
|
|
(define scale (/ size (· this font unitsPerEm)))
|
|
|
|
|
(* width scale)]
|
|
|
|
|
[else (send (· this font) measure-string string size)]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; called from text.rkt
|
|
|
|
|
(define/contract (encode this text [features #f])
|
|
|
|
|
((string?) ((or/c list? #f)) . ->*m .
|
|
|
|
|
(list/c (listof string?) (listof (is-a?/c GlyphPosition))))
|
|
|
|
|
((string?) ((option/c list?)) . ->*m .
|
|
|
|
|
(list/c (listof string?) (listof GlyphPosition?)))
|
|
|
|
|
(define glyphRun (send (· this font) layout text features))
|
|
|
|
|
(define glyphs (· glyphRun glyphs))
|
|
|
|
|
(for ([g (in-list glyphs)])
|
|
|
|
@ -65,8 +63,12 @@ For now, we'll just measure width of the characters.
|
|
|
|
|
(values subset-idx posn)))
|
|
|
|
|
(list subset-idxs new-positions))
|
|
|
|
|
|
|
|
|
|
(require racket/runtime-path)
|
|
|
|
|
(define-runtime-path charter-path "test/assets/charter.ttf")
|
|
|
|
|
|
|
|
|
|
(define-macro (sum-flags [COND VAL] ...)
|
|
|
|
|
#'(for/sum ([c (in-list (list COND ...))]
|
|
|
|
|
[v (in-list (list VAL ...))]
|
|
|
|
|
#:when c)
|
|
|
|
|
v))
|
|
|
|
|
|
|
|
|
|
(define/contract (embed this)
|
|
|
|
|
(->m void?)
|
|
|
|
@ -83,12 +85,16 @@ For now, we'll just measure width of the characters.
|
|
|
|
|
0)])
|
|
|
|
|
(floor (/ val 256)))) ; equivalent to >> 8
|
|
|
|
|
|
|
|
|
|
(define flags (+
|
|
|
|
|
(if (not (zero? (· this font post isFixedPitch))) (expt 2 0) 0)
|
|
|
|
|
(if (<= 1 familyClass 7) (expt 2 1) 0)
|
|
|
|
|
(expt 2 2) ; assume the font uses non-latin characters
|
|
|
|
|
(if (= familyClass 10) (expt 2 3) 0)
|
|
|
|
|
(if (· this font head macStyle italic) (expt 2 6) 0)))
|
|
|
|
|
;; font descriptor flags
|
|
|
|
|
(match-define (list FIXED_PITCH SERIF SYMBOLIC SCRIPT _UNUSED NONSYMBOLIC ITALIC)
|
|
|
|
|
(map (curry expt 2) (range 7)))
|
|
|
|
|
|
|
|
|
|
(define flags (sum-flags
|
|
|
|
|
[(not (zero? (· this font post isFixedPitch))) FIXED_PITCH]
|
|
|
|
|
[(<= 1 familyClass 7) SERIF]
|
|
|
|
|
[#t SYMBOLIC] ; assume the font uses non-latin characters
|
|
|
|
|
[(= familyClass 10) SCRIPT]
|
|
|
|
|
[(· this font head macStyle italic) ITALIC]))
|
|
|
|
|
|
|
|
|
|
;; generate a random tag (6 uppercase letters. 65 is the char code for 'A')
|
|
|
|
|
(when (test-mode) (random-seed 0))
|
|
|
|
@ -159,28 +165,32 @@ For now, we'll just measure width of the characters.
|
|
|
|
|
(toHex value)))
|
|
|
|
|
(format "<~a>" (string-join encoded " "))))
|
|
|
|
|
|
|
|
|
|
(send cmap end @string-append{
|
|
|
|
|
/CIDInit /ProcSet findresource begin
|
|
|
|
|
12 dict begin
|
|
|
|
|
begincmap
|
|
|
|
|
/CIDSystemInfo <<
|
|
|
|
|
(define unicode-cmap-str #<<HERE
|
|
|
|
|
/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><ffff>
|
|
|
|
|
endcodespacerange
|
|
|
|
|
1 beginbfrange
|
|
|
|
|
<0000> <@(toHex (sub1 (length entries)))> [@(string-join entries " ")]
|
|
|
|
|
endbfrange
|
|
|
|
|
endcmap
|
|
|
|
|
CMapName currentdict /CMap defineresource pop
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
>> def
|
|
|
|
|
/CMapName /Adobe-Identity-UCS def
|
|
|
|
|
/CMapType 2 def
|
|
|
|
|
1 begincodespacerange
|
|
|
|
|
<0000><ffff>
|
|
|
|
|
endcodespacerange
|
|
|
|
|
1 beginbfrange
|
|
|
|
|
<0000> <~a> [~a]
|
|
|
|
|
endbfrange
|
|
|
|
|
endcmap
|
|
|
|
|
CMapName currentdict /CMap defineresource pop
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
HERE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
(send cmap end (format unicode-cmap-str (toHex (sub1 (length entries))) (string-join entries " ")))
|
|
|
|
|
|
|
|
|
|
#;(report (· cmap toString) 'cmap-id)
|
|
|
|
|
cmap)
|
|
|
|
|
|
|
|
|
|