From 488b208f85b94f9bf821079ba7670e0c7a9c4c5f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 2 Apr 2020 17:31:15 -0700 Subject: [PATCH] support pica measurement --- quad/quad/scribblings/quad.scrbl | 2 +- quad/quadwriter/attrs.rkt | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/quad/quad/scribblings/quad.scrbl b/quad/quad/scribblings/quad.scrbl index 07ceb4ae..f72003ae 100644 --- a/quad/quad/scribblings/quad.scrbl +++ b/quad/quad/scribblings/quad.scrbl @@ -473,7 +473,7 @@ A text-drawing quad will also inherit the current @secref["Font_attributes"], wh These are the attributes that can be used inside a @tech{Q-expression} passed to @racketmodname[quadwriter]. Inside a Q-expression, every attribute is a @tech[#:doc '(lib "scribblings/guide/guide.scrbl")]{symbol}, and every attribute value is a @tech[#:doc '(lib "scribblings/guide/guide.scrbl")]{string}. -A @deftech{dimension string} represents a distance in the plane. If unitless, it is treated as points (where 1 point = 1/72 of an inch). If the number has @racket[in], @racket[cm], or @racket[mm] as a suffix, it is treated as inches, centimeters, or millimeters respectively. If the number has @racket[em] as a suffix, it is treated as an em measurement, which is a multiple of the current font size. +A @deftech{dimension string} represents a distance in the plane. If unitless, it is treated as points (where 1 point = 1/72 of an inch). If the number has @racket[in], @racket[cm], or @racket[mm] as a suffix, it is treated as inches, centimeters, or millimeters respectively. If the number has @racket[p] or @racket[pica] as a suffix or infix, it is treated as a pica / point measurement. A pica is 12 points, so @racket[12p] is 72 points, and @racket[3p9] is 45 points. If the number has @racket[em] as a suffix, it is treated as an em measurement, which is a multiple of the current font size. @subsubsection{Document-level attributes} diff --git a/quad/quadwriter/attrs.rkt b/quad/quadwriter/attrs.rkt index 3eae00b9..a3dd51bd 100644 --- a/quad/quadwriter/attrs.rkt +++ b/quad/quadwriter/attrs.rkt @@ -9,28 +9,30 @@ (for/list ([kv (in-slice 2 kvs)]) kv)) -(define (cm->in x) (/ x 2.54)) -(define (in->pts x) (* 72 x)) -(define (mm->cm x) (/ x 10.0)) +(define (picas->pts left right) (+ (* left 12) (or right 0))) +(define (cm->in x _) (/ x 2.54)) +(define (in->pts x _) (* 72 x)) +(define (mm->cm x _) (/ x 10.0)) (define (parse-dimension x [em-resolution-attrs #false]) (match x [#false #false] [(? number? num) num] [(? string? str) - (match (regexp-match #px"^(-?[0-9\\.]+)\\s*([a-z]+)$" (string-downcase str)) + (match (regexp-match #px"^(-?[0-9\\.]+)\\s*([a-z]+)([0-9\\.]*)$" (string-downcase str)) [#false str] ; a string other than a dimension string, so leave it - [(list str num-string unit) + [(list str num-string-left unit num-string-right) ((match unit [(regexp #rx"(pt|point)(s)?$") values] ; points + [(regexp #rx"(p|pica)(s)?") picas->pts] ; picas [(regexp #rx"in(ch(es)?)?$") in->pts] ; inches [(regexp #rx"cms?$") (compose1 in->pts cm->in)] ; cm [(regexp #rx"mms?$") (compose1 in->pts cm->in mm->cm)] ; mm [(regexp #rx"ems?$") (if em-resolution-attrs - (λ (num) (* (hash-ref em-resolution-attrs :font-size) num)) + (λ (num _) (* (hash-ref em-resolution-attrs :font-size) num)) ;; if we don't have attrs for resolving the em string, we leave it alone - (λ (num) str))] ; em - [_ (raise-argument-error 'parse-dimension "dimension string" str)]) (string->number num-string))])])) + (λ (num _) str))] ; em + [_ (raise-argument-error 'parse-dimension "dimension string" str)]) (string->number num-string-left) (string->number num-string-right))])])) (define (copy-block-attrs source-hash dest-hash) (define new-hash (make-hasheq)) @@ -243,7 +245,8 @@ Naming guidelines :pdf-subject :pdf-author :pdf-keywords - :string)) #true)) + :string + :footer-text)) #true)) (define (takes-path? k) (and (memq k (list :image-file)) #true))