From 3b3eac5c4bd9f2f21f4e29a30b83b7e3761255c3 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 6 Jun 2019 11:20:52 -0700 Subject: [PATCH] support hard linebreaks (fixes #17) --- quad/quadwriter/render.rkt | 34 ++++++++++++++++++---------------- quad/quadwriter/tags.rkt | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/quad/quadwriter/render.rkt b/quad/quadwriter/render.rkt index 4ae7425f..3465b90b 100644 --- a/quad/quadwriter/render.rkt +++ b/quad/quadwriter/render.rkt @@ -36,10 +36,10 @@ [(_ ALL-BREAKS-ID . TYPES) (with-syntax ([((TYPE-BREAK TYPE-STR Q:TYPE-BREAK) ...) (for/list ([type (in-list (syntax->list #'TYPES))]) - (list - (format-id #'TYPES "~a-break" type) - (symbol->string (syntax->datum type)) - (format-id #'TYPES "q:~a-break" type)))]) + (list + (format-id #'TYPES "~a-break" type) + (symbol->string (syntax->datum type)) + (format-id #'TYPES "q:~a-break" type)))]) #'(begin (define TYPE-BREAK '(q ((break TYPE-STR)))) ... (define ALL-BREAKS-ID (list (cons TYPE-BREAK Q:TYPE-BREAK) ...))))])) @@ -58,15 +58,17 @@ ;; do this before ->string-quad so that it can handle the sizing promises (apply append (for/list ([q (in-list qs)]) - (match (quad-ref q :hyphenate) - [#false (list q)] - [_ (for*/list ([str (in-list (quad-elems q))] - [hyphen-char (in-value #\u00AD)] - [hstr (in-value (hyphenate str hyphen-char - #:min-left-length 3 - #:min-right-length 3))] - [substr (in-list (regexp-match* (regexp (string hyphen-char)) hstr #:gap-select? #t))]) - (struct-copy quad q [elems (list substr)]))])))) + (match (quad-ref q :hyphenate) + [#true #:when (and (pair? (quad-elems q)) + (andmap string? (quad-elems q))) + (for*/list ([str (in-list (quad-elems q))] + [hyphen-char (in-value #\u00AD)] + [hstr (in-value (hyphenate str hyphen-char + #:min-left-length 3 + #:min-right-length 3))] + [substr (in-list (regexp-match* (regexp (string hyphen-char)) hstr #:gap-select? #t))]) + (struct-copy quad q [elems (list substr)]))] + [_ (list q)])))) (define (handle-cascading-attrs attrs) (resolve-font-path attrs) @@ -98,9 +100,9 @@ ;; page size can be specified by name, or measurements. ;; explicit measurements from page-height and page-width supersede those from page-size. (match-define (list page-width page-height) (for/list ([k (list :page-width :page-height)]) - (match (quad-ref (car qs) k) - [#false #false] - [val (parse-dimension val 'round)]))) + (match (quad-ref (car qs) k) + [#false #false] + [val (parse-dimension val 'round)]))) ;; `make-pdf` will sort out conflicts among page dimensions (make-pdf #:compress compress? #:auto-first-page #false diff --git a/quad/quadwriter/tags.rkt b/quad/quadwriter/tags.rkt index dab27417..7f7e127f 100644 --- a/quad/quadwriter/tags.rkt +++ b/quad/quadwriter/tags.rkt @@ -26,6 +26,8 @@ :display (symbol->string (gensym))) attrs) exprs)) +(define-tag-function (br attrs exprs) line-break) + (define-tag-function (hr attrs exprs) hr-break) (define-tag-function (blockquote attrs exprs)