wrap: faster (and simpler) wrapping algorithm.
parent
6d5775e732
commit
3f8c9e4150
|
@ -39,7 +39,7 @@ word wrap."
|
||||||
[ "hello how are you today?" 3 wrap-string ] unit-test
|
[ "hello how are you today?" 3 wrap-string ] unit-test
|
||||||
|
|
||||||
{ "aaa\nbb cc\nddddd" } [ "aaa bb cc ddddd" 6 wrap-string ] unit-test
|
{ "aaa\nbb cc\nddddd" } [ "aaa bb cc ddddd" 6 wrap-string ] unit-test
|
||||||
{ "aaa\nbb ccc\ndddddd" } [ "aaa bb ccc dddddd" 6 wrap-string ] unit-test
|
{ "aaa bb\nccc\ndddddd" } [ "aaa bb ccc dddddd" 6 wrap-string ] unit-test
|
||||||
{ "aaa bb\ncccc\nddddd" } [ "aaa bb cccc ddddd" 6 wrap-string ] unit-test
|
{ "aaa bb\ncccc\nddddd" } [ "aaa bb cccc ddddd" 6 wrap-string ] unit-test
|
||||||
{ "aaa bb\nccccccc\nddddddd" } [ "aaa bb ccccccc ddddddd" 6 wrap-string ] unit-test
|
{ "aaa bb\nccccccc\nddddddd" } [ "aaa bb ccccccc ddddddd" 6 wrap-string ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -9,3 +9,15 @@ ARTICLE: "wrap" "Word wrapping"
|
||||||
"The " { $vocab-link "wrap" } " vocabulary implements word wrapping. Wrapping can take place based on simple strings, assumed to be monospace, or abstract word objects."
|
"The " { $vocab-link "wrap" } " vocabulary implements word wrapping. Wrapping can take place based on simple strings, assumed to be monospace, or abstract word objects."
|
||||||
{ $vocab-subsection "String word wrapping" "wrap.strings" }
|
{ $vocab-subsection "String word wrapping" "wrap.strings" }
|
||||||
{ $vocab-subsection "Word object wrapping" "wrap.words" } ;
|
{ $vocab-subsection "Word object wrapping" "wrap.words" } ;
|
||||||
|
|
||||||
|
HELP: element
|
||||||
|
{ $class-description "An element to be wrapped. It has the following slots:" }
|
||||||
|
{ $table
|
||||||
|
{ { $slot "contents" } "The object being wrapped." }
|
||||||
|
{ { $slot "black" } "The width of the object (e.g., the text length)." }
|
||||||
|
{ { $slot "white" } "The space after the object (e.g., trailing whitespace)." }
|
||||||
|
} ;
|
||||||
|
|
||||||
|
HELP: wrap
|
||||||
|
{ $values { "elements" { $sequence element } } { "width" real } }
|
||||||
|
{ $description "Break the " { $snippet "elements" } " into lines such that the total width of each line tries to be less than " { $snippet "width" } " while attempting to minimize the raggedness represented by the amount of space at the end of each line. Returns an array of lines." } ;
|
||||||
|
|
|
@ -1,72 +1,46 @@
|
||||||
! Copyright (C) 2009 Daniel Ehrenberg
|
! Copyright (C) 2009 Daniel Ehrenberg
|
||||||
|
! Copyright (C) 2017 John Benediktsson
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors arrays combinators combinators.short-circuit
|
USING: accessors arrays kernel locals math sequences
|
||||||
fry kernel kernel.private lists locals math sequences typed ;
|
sequences.private ;
|
||||||
IN: wrap
|
IN: wrap
|
||||||
|
|
||||||
! black is the text length, white is the whitespace length
|
|
||||||
TUPLE: element contents black white ;
|
TUPLE: element contents black white ;
|
||||||
|
|
||||||
C: <element> element
|
C: <element> element
|
||||||
|
|
||||||
<PRIVATE
|
:: wrap ( elements width -- array )
|
||||||
|
elements length integer>fixnum-strict :> #elements
|
||||||
|
elements [ black>> ] { } map-as :> black
|
||||||
|
elements [ white>> ] { } map-as :> white
|
||||||
|
|
||||||
: element-length ( element -- n )
|
#elements 1 + f <array> :> minima
|
||||||
[ black>> ] [ white>> ] bi + ; inline
|
#elements 1 + 0 <array> :> breaks
|
||||||
|
|
||||||
TUPLE: paragraph line-max lines head-width tail-cost ;
|
0 0 minima set-nth-unsafe
|
||||||
C: <paragraph> paragraph
|
|
||||||
|
|
||||||
: if-one-line ( paragraph true false -- )
|
minima [| base i |
|
||||||
[ dup lines>> 1list? ] 2dip if ; inline
|
0 i 1 + [ dup #elements <= ] [| j |
|
||||||
|
j 1 - black nth-unsafe + dup :> w
|
||||||
|
j 1 - white nth-unsafe +
|
||||||
|
|
||||||
TYPED: top-fits? ( paragraph: paragraph -- ? )
|
w width > [
|
||||||
[ head-width>> ] [ line-max>> ] bi <= ; inline
|
j 1 - i = [
|
||||||
|
0 j minima set-nth-unsafe
|
||||||
|
i j breaks set-nth-unsafe
|
||||||
|
] when #elements
|
||||||
|
] [
|
||||||
|
base
|
||||||
|
j #elements = [ width w - sq + ] unless :> cost
|
||||||
|
j minima nth-unsafe [ cost >= ] [ t ] if* [
|
||||||
|
cost j minima set-nth-unsafe
|
||||||
|
i j breaks set-nth-unsafe
|
||||||
|
] when j
|
||||||
|
] if 1 +
|
||||||
|
] while 2drop
|
||||||
|
] each-index
|
||||||
|
|
||||||
TYPED: fits? ( paragraph: paragraph -- ? )
|
#elements [ dup 0 > ] [
|
||||||
! Make this not count spaces at end
|
[ breaks nth dup ] keep elements <slice>
|
||||||
{ [ lines>> car 1list? ] [ top-fits? ] } 1|| ; inline
|
[ contents>> ] map
|
||||||
|
] produce nip reverse ;
|
||||||
TYPED: paragraph-cost ( paragraph: paragraph -- cost )
|
|
||||||
[ drop 0 ] [
|
|
||||||
[ head-width>> ] [ line-max>> - sq ] [ tail-cost>> ] tri +
|
|
||||||
] if-one-line ; inline
|
|
||||||
|
|
||||||
: min-cost ( paragraphs -- paragraph )
|
|
||||||
[ paragraph-cost ] infimum-by ; inline
|
|
||||||
|
|
||||||
TYPED: new-line ( paragraph: paragraph element: element -- paragraph )
|
|
||||||
{
|
|
||||||
[ drop line-max>> ]
|
|
||||||
[ [ lines>> ] [ 1list ] bi* swons ]
|
|
||||||
[ nip black>> ]
|
|
||||||
[ drop paragraph-cost ]
|
|
||||||
} 2cleave <paragraph> ; inline
|
|
||||||
|
|
||||||
TYPED: add-element ( paragraph: paragraph element: element -- )
|
|
||||||
[ '[ _ element-length + ] change-head-width ]
|
|
||||||
[ '[ unswons _ swons swons ] change-lines ] bi drop ; inline
|
|
||||||
|
|
||||||
TYPED: wrap-step ( paragraphs: array element: element -- paragraphs )
|
|
||||||
[ [ min-cost ] dip new-line ]
|
|
||||||
[ dupd '[ _ add-element ] each ]
|
|
||||||
2bi swap prefix { array } declare
|
|
||||||
[ fits? ] filter ; inline
|
|
||||||
|
|
||||||
: 1paragraph ( line-max element -- paragraph )
|
|
||||||
[ 1list 1list ] [ black>> ] bi 0 <paragraph> ;
|
|
||||||
|
|
||||||
: post-process ( paragraph -- array )
|
|
||||||
lines>> [ [ contents>> ] lmap>array ] lmap>array ;
|
|
||||||
|
|
||||||
: initial-step ( line-max elements -- elements paragraph )
|
|
||||||
reverse unclip swapd 1paragraph 1array ;
|
|
||||||
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
: wrap ( elements width -- array )
|
|
||||||
swap [ drop { } ] [
|
|
||||||
initial-step
|
|
||||||
[ wrap-step ] reduce
|
|
||||||
min-cost
|
|
||||||
post-process
|
|
||||||
] if-empty ;
|
|
||||||
|
|
Loading…
Reference in New Issue