1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays alien alien.c-types alien.data alien.syntax kernel
4 destructors accessors fry words hashtables strings sequences
5 memoize assocs make math math.order math.vectors math.rectangles
6 math.functions locals init namespaces combinators fonts colors
7 cache core-foundation core-foundation.strings
8 core-foundation.attributed-strings core-foundation.utilities
9 core-graphics core-graphics.types core-text.fonts ;
12 TYPEDEF: void* CTLineRef
14 C-GLOBAL: CFStringRef kCTFontAttributeName
15 C-GLOBAL: CFStringRef kCTKernAttributeName
16 C-GLOBAL: CFStringRef kCTLigatureAttributeName
17 C-GLOBAL: CFStringRef kCTForegroundColorAttributeName
18 C-GLOBAL: CFStringRef kCTParagraphStyleAttributeName
19 C-GLOBAL: CFStringRef kCTUnderlineStyleAttributeName
20 C-GLOBAL: CFStringRef kCTVerticalFormsAttributeName
21 C-GLOBAL: CFStringRef kCTGlyphInfoAttributeName
23 FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
25 FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
27 FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
29 FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
31 FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
33 FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
37 ERROR: not-a-string object ;
39 MEMO: make-attributes ( open-font color -- hashtable )
41 kCTForegroundColorAttributeName ,,
42 kCTFontAttributeName ,,
45 : <CTLine> ( string open-font color -- line )
48 dup selection? [ string>> ] when
49 dup string? [ not-a-string ] unless
51 make-attributes <CFAttributedString> &CFRelease
52 CTLineCreateWithAttributedString
55 TUPLE: line < disposable font string line metrics image loc dim
56 render-loc render-dim ;
58 : typographic-bounds ( line -- width ascent descent leading )
59 { CGFloat CGFloat CGFloat }
60 [ CTLineGetTypographicBounds ] with-out-parameters ; inline
62 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
70 : compute-font-metrics ( metrics font -- metrics )
71 [ CTFontGetCapHeight >>cap-height ]
72 [ CTFontGetXHeight >>x-height ]
75 : compute-line-metrics ( open-font line -- line-metrics )
77 [ compute-font-metrics ]
78 [ typographic-bounds store-typographic-bounds ] bi*
81 : metrics>dim ( bounds -- dim )
82 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
86 : fill-background ( context font dim -- )
87 [ background>> >rgba-components CGContextSetRGBFillColor ]
88 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
91 : selection-rect ( dim line selection -- rect )
92 [ start>> ] [ end>> ] bi
93 [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
94 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
96 : CGRect-translate-x ( CGRect x -- CGRect' )
97 [ dup CGRect-x ] dip - over set-CGRect-x ;
99 :: fill-selection-background ( context loc dim line string -- )
101 context string color>> >rgba-components CGContextSetRGBFillColor
102 context dim line string selection-rect
103 loc first CGRect-translate-x
107 : line-rect ( line -- rect )
108 dummy-context CTLineGetImageBounds ;
110 : set-text-position ( context loc -- )
111 first2 [ neg ] bi@ CGContextSetTextPosition ;
113 :: line-loc ( metrics loc dim -- loc )
115 metrics ascent>> ceiling dim second loc second + - 2array ;
117 :: <line> ( font string -- line )
120 font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font
121 string open-font font foreground>> <CTLine> |CFRelease :> line
122 open-font line compute-line-metrics
123 [ >>metrics ] [ metrics>dim >>dim ] bi
129 :: render ( line -- line image )
130 line line>> :> ctline
131 line string>> :> string
136 ctline line-rect :> rect
137 rect origin>> CGPoint>loc :> (loc)
138 rect size>> CGSize>dim :> (dim)
139 (loc) [ floor ] map :> loc
140 (loc) (dim) [ + ceiling ] 2map :> ext
141 ext loc [ - >integer 1 max ] 2map :> dim
143 loc line render-loc<<
144 dim line render-dim<<
146 line metrics>> loc dim line-loc line loc<<
150 line render-loc>> :> loc
151 line render-dim>> :> dim
155 [ font dim fill-background ]
156 [ loc dim ctline string fill-selection-background ]
157 [ loc set-text-position ]
158 [ [ ctline ] dip CTLineDraw ]
160 ] make-bitmap-image retina? get-global >>2x? ;
162 : line>image ( line -- image )
163 dup image>> [ render >>image ] unless image>> ;
165 M: line dispose* line>> CFRelease ;
169 : cached-line ( font string -- line )
170 cached-lines get-global [ <line> ] 2cache ;
172 [ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook