line alignment, naive

main
Matthew Butterick 5 years ago
parent ae4d53a787
commit 25871f2723

@ -1,104 +1,10 @@
#lang qtest/markdown #lang qtest/markdown
> A simple _hyphenation engine_ that **uses** the _**KnuthLiang**_ hyphenation algorithm originally developed for TeX. A simple _hyphenation engine_ that uses the KnuthLiang hyphenation algorithm originally developed for TeX. hello world hello world hello world hello world hello world hello world hello world
# Hyphenate
A simple _hyphenation engine_ that uses the _**KnuthLiang**_ hyphenation algorithm originally developed for TeX.
I **have added little** to their work. Accordingly, I take no credit, except a spoonful of *snako-bits.*
And now, for something __altogether__ the same. Yes! No?!ß
## 1. Installation
At the command line:
* do this. Then do another thing. Then something else, please. Then something else, please.
* Finally, that.
We said `raco pkg install hyphenate` dude
```
it's a
codeblock!
```
> A simple _hyphenation engine_ that **uses** the _**KnuthLiang**_ hyphenation algorithm originally developed for TeX. A simple _hyphenation engine_ that uses the KnuthLiang hyphenation algorithm originally developed for TeX.
> hello world hello world hello world hello world hello world hello world hello world
``` ```
it's a hello world hello world
codeblock! hello world
``` ```
> A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text.
A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text.
A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text.
A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text. A [list of web colors](https://en.wikipedia.org/wiki/Web_colors).
Certain word processors allow users to [insert soft
hyphens](http://practicaltypography.com/optional-hyphens.html) in their
text.

@ -25,6 +25,7 @@
(define-tag-function (blockquote attrs exprs) (define-tag-function (blockquote attrs exprs)
(qexpr (append '((display "block") (qexpr (append '((display "block")
(line-align "center")
(background-color "#eee") (background-color "#eee")
(font-family "fira") (font-size "10") (line-height "15") (font-family "fira") (font-size "10") (line-height "15")
(border-width-top "0.5") (border-color-top "gray") (border-inset-top "8") (border-width-top "0.5") (border-color-top "gray") (border-inset-top "8")
@ -63,7 +64,7 @@
(define h6 h3) (define h6 h3)
(define-tag-function (code attrs exprs) (define-tag-function (code attrs exprs)
(qexpr (append '((font-family "fira-mono")(font-size "10")(bg "aliceblue")) attrs) exprs)) (qexpr (append '((font-family "fira-mono")(line-align "right")(font-size "10")(bg "aliceblue")) attrs) exprs))
(define-tag-function (pre attrs exprs) (define-tag-function (pre attrs exprs)
;; pre needs to convert white space to equivalent layout elements ;; pre needs to convert white space to equivalent layout elements
@ -82,7 +83,7 @@
(define draw-debug? #t) (define draw-debug? #t)
(define draw-debug-line? #f) (define draw-debug-line? #t)
(define draw-debug-block? #t) (define draw-debug-block? #t)
(define draw-debug-string? #f) (define draw-debug-string? #f)
@ -220,7 +221,8 @@
border-width-left border-width-right border-width-top border-width-bottom border-width-left border-width-right border-width-top border-width-bottom
border-color-left border-color-right border-color-top border-color-bottom border-color-left border-color-right border-color-top border-color-bottom
background-color background-color
keep-lines keep-first keep-last keep-all keep-with-next)) keep-lines keep-first keep-last keep-all keep-with-next
line-align line-align-first line-align-last))
(for* ([k (in-list block-attrs)] (for* ([k (in-list block-attrs)]
[v (in-value (hash-ref source-hash k #f))] [v (in-value (hash-ref source-hash k #f))]
#:when v) #:when v)
@ -255,17 +257,34 @@
(match (consolidate-runs pcs) (match (consolidate-runs pcs)
[(? pair? elems) [(? pair? elems)
(define elem (unsafe-car elems)) (define elem (unsafe-car elems))
(match-define (list line-width line-height) (quad-size q:line))
(define new-size (let ()
(define line-heights
(filter-map (λ (q) (quad-ref q 'line-height)) pcs))
(pt line-width (if (empty? line-heights) line-height (apply max line-heights)))))
(list (struct-copy quad q:line (list (struct-copy quad q:line
;; move block attrs up, so they are visible in page wrap ;; move block attrs up, so they are visible in page wrap
[attrs (copy-block-attrs (quad-attrs elem) [attrs (copy-block-attrs (quad-attrs elem)
(hash-copy (quad-attrs q:line)))] (hash-copy (quad-attrs q:line)))]
;; line width is static ;; line width is static
;; line height is the max 'line-height value or the natural height of q:line ;; line height is the max 'line-height value or the natural height of q:line
[size (let () [size new-size]
(define line-heights #|
(filter-map (λ (q) (quad-ref q 'line-height)) pcs)) line alignment
(match-define (list w h) (quad-size q:line)) naive approach works but:
(pt w (if (empty? line-heights) h (apply max line-heights))))] + `line-width` is not the right reference value for calculating offset adjust
+ alignment width reference needs to take account of inset due to border
|#
[offset (match (quad-ref elem 'line-align "left")
["left" (pt 0 0)]
[val (define elems-width (pt-x (apply pt+ (map size elems))))
(define h-divisor (match val
["right" 1]
["center" 2]))
(define offset-adjust
(pt (/ (- line-width elems-width) h-divisor) 0))
(pt+ offset-adjust (quad-offset q:line))])]
;; handle list indexes. drop new quad into line to hold list index ;; handle list indexes. drop new quad into line to hold list index
;; could also use this for line numbers ;; could also use this for line numbers
[elems (append [elems (append
@ -362,8 +381,8 @@
(match-define (list bil bit bir bib) (match-define (list bil bit bir bib)
(map (λ (k) (quad-ref first-line k 0)) (map (λ (k) (quad-ref first-line k 0))
'(border-inset-left border-inset-top border-inset-right border-inset-bottom))) '(border-inset-left border-inset-top border-inset-right border-inset-bottom)))
(match-define (list left top) (map + (quad-origin q) (list bil bit))) (match-define (list left top) (pt+ (quad-origin q) (list bil bit)))
(match-define (list width height) (map - (size q) (list (+ bil bir) (+ bit bib)))) (match-define (list width height) (pt- (size q) (list (+ bil bir) (+ bit bib))))
;; fill rect ;; fill rect
(cond (cond
[(quad-ref first-line 'background-color) [(quad-ref first-line 'background-color)

@ -35,18 +35,22 @@
;; and compare them key-by-key ;; and compare them key-by-key
(hashes-equal? (quad-attrs q1) (quad-attrs q2)))) (hashes-equal? (quad-attrs q1) (quad-attrs q2))))
(struct quad (attrs (struct quad (attrs ; key-value pairs, arbitrary
elems elems ; subquads or text
size ;; size is a two-dim pt
in size ; outer size of quad for layout (though not necessarily the bounding box for drawing)
out ;; in, out, inner are phrased in terms of cardinal position
inner in ; alignment point matched to previous quad
offset out ; alignment point matched to next quad
origin inner ; alignment point for elems (might be different from in/out)
printable ;; offset, origin are two-dim pts
draw-start offset ; relocation of pen before quad is drawn
draw origin ; reference point for all subsequent drawing ops in the quad. Calculated, not set directly
draw-end) printable ; whether the quad will print
draw-start ; func called at the beginning of every draw event (for setup ops)
draw ; func called in the middle of every daw event
draw-end ; func called at the end of every draw event (for teardown ops)
)
#:transparent #:transparent
#:property prop:custom-write #:property prop:custom-write
(λ (v p w?) (display (λ (v p w?) (display

Loading…
Cancel
Save