1 ! Copyright (C) 2011-2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors assocs colors.constants combinators fonts fry
5 io io.styles kernel math math.order pdf.text pdf.wrap sequences
10 TUPLE: margin left right top bottom ;
14 TUPLE: canvas x y width height margin col-width font stream
15 foreground background page-color inset line-height metrics ;
17 : <canvas> ( -- canvas )
23 54 54 54 54 <margin> >>margin
25 sans-serif-font 12 >>size >>font
29 dup font>> font-metrics >>metrics ;
31 : set-style ( canvas style -- canvas )
34 font-name of "sans-serif" or {
35 { "sans-serif" [ "Helvetica" ] }
36 { "serif" [ "Times" ] }
37 { "monospace" [ "Courier" ] }
38 [ " is unsupported" append throw ]
39 } case [ dup font>> ] dip >>name drop
43 [ dup font>> ] dip >>size drop
46 font-style of [ dup font>> ] dip {
49 { bold-italic [ t t ] }
51 } case [ >>bold? ] [ >>italic? ] bi* drop
53 [ foreground of COLOR: black or >>foreground ]
54 [ background of f or >>background ]
55 [ page-color of f or >>page-color ]
56 [ inset of { 0 0 } or >>inset ]
58 dup font>> font-metrics
59 [ >>metrics ] [ height>> '[ _ max ] change-line-height ] bi ;
61 ! introduce positioning of elements versus canvas?
63 : margin-x ( canvas -- n )
64 margin>> [ left>> ] [ right>> ] bi + ;
66 : margin-y ( canvas -- n )
67 margin>> [ top>> ] [ bottom>> ] bi + ;
69 : (width) ( canvas -- n )
70 [ width>> ] [ margin>> [ left>> ] [ right>> ] bi + ] bi - ;
72 : width ( canvas -- n )
73 [ (width) ] [ col-width>> ] bi min ;
75 : height ( canvas -- n )
76 [ height>> ] [ margin>> [ top>> ] [ bottom>> ] bi + ] bi - ;
79 [ margin>> left>> ] [ x>> ] bi + ;
82 [ height>> ] [ margin>> top>> ] [ y>> ] tri + - ;
84 : inc-x ( canvas n -- )
85 '[ _ + ] change-x drop ;
87 : inc-y ( canvas n -- )
88 '[ _ + ] change-y drop ;
90 : line-height ( canvas -- n )
91 [ line-height>> ] [ inset>> first 2 * ] bi + ;
93 : line-break ( canvas -- )
94 [ line-height>> ] keep [ + ] change-y 0 >>x
95 dup metrics>> height>> >>line-height drop ;
97 : ?line-break ( canvas -- )
98 dup x>> 0 > [ line-break ] [ drop ] if ;
100 : ?break ( canvas -- )
101 dup x>> 0 > [ ?line-break ] [
102 [ 7 + ] change-y 0 >>x drop
105 : inc-lines ( canvas n -- )
106 [ 0 >>x ] dip [ dup line-break ] times drop ;
108 : avail-width ( canvas -- n )
109 [ width ] [ x>> ] bi [-] ;
111 : avail-height ( canvas -- n )
112 [ height ] [ y>> ] bi [-] ;
114 : avail-lines ( canvas -- n )
115 [ avail-height ] [ line-height>> ] bi /i ; ! FIXME: 1 +
117 : text-fits? ( canvas string -- ? )
118 [ dup font>> ] [ word-split1 drop ] bi*
119 text-width swap avail-width <= ;
121 : draw-page-color ( canvas -- ) ! FIXME:
125 [ 0 0 ] dip [ width>> ] [ height>> ] bi
129 : draw-background ( canvas line -- )
133 [ drop [ x ] [ y ] bi ]
134 [ [ font>> ] [ text-dim first2 neg ] bi* ] 2bi
138 : draw-text1 ( canvas line -- canvas )
139 [ draw-background ] [
141 over font>> text-size
142 over foreground>> [ foreground-color ] when*
143 over [ x ] [ y ] [ metrics>> ascent>> - ] tri text-location
144 over dup font>> pick text-width inc-x
149 : draw-text ( canvas lines -- )
152 [ [ draw-text1 dup line-break ] each ]
153 [ [ draw-text1 ] when* ] bi* drop
156 : draw-line ( canvas width -- )
157 swap [ x ] [ y ] [ line-height>> 2 / - ] tri
158 [ line-move ] [ [ + ] [ line-line ] bi* ] 2bi