1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays alien alien.c-types alien.syntax kernel
4 destructors accessors fry words hashtables strings
5 sequences memoize assocs math math.functions locals init
6 namespaces combinators fonts colors core-foundation
7 core-foundation.strings core-foundation.attributed-strings
8 core-foundation.utilities core-graphics core-graphics.types
9 core-text.fonts core-text.utilities ;
12 TYPEDEF: void* CTLineRef
14 C-GLOBAL: kCTFontAttributeName
15 C-GLOBAL: kCTKernAttributeName
16 C-GLOBAL: kCTLigatureAttributeName
17 C-GLOBAL: kCTForegroundColorAttributeName
18 C-GLOBAL: kCTParagraphStyleAttributeName
19 C-GLOBAL: kCTUnderlineStyleAttributeName
20 C-GLOBAL: kCTVerticalFormsAttributeName
21 C-GLOBAL: 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 ) ;
35 ERROR: not-a-string object ;
37 : <CTLine> ( string open-font color -- line )
40 dup selection? [ string>> ] when
41 dup string? [ not-a-string ] unless
44 kCTForegroundColorAttributeName set
45 kCTFontAttributeName set
46 ] H{ } make-assoc <CFAttributedString> &CFRelease
47 CTLineCreateWithAttributedString
50 TUPLE: line font line metrics dim bitmap age refs disposed ;
52 : compute-line-metrics ( line -- line-metrics )
53 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
54 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
57 : bounds>dim ( bounds -- dim )
58 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
62 : fill-background ( context font dim -- )
63 [ background>> >rgba-components CGContextSetRGBFillColor ]
64 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
67 : selection-rect ( dim line selection -- rect )
68 [ start>> ] [ end>> ] bi
69 [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
70 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
72 :: fill-selection-background ( context dim line string -- )
74 context string color>> >rgba-components CGContextSetRGBFillColor
75 context dim line string selection-rect CGContextFillRect
78 : set-text-position ( context metrics -- )
79 [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
81 :: <line> ( font string -- line )
83 [let* | open-font [ font cache-font CFRetain |CFRelease ]
84 line [ string open-font font foreground>> <CTLine> |CFRelease ]
85 metrics [ line compute-line-metrics ]
86 dim [ metrics bounds>dim ] |
89 [ font dim fill-background ]
90 [ dim line string fill-selection-background ]
91 [ metrics set-text-position ]
92 [ [ line ] dip CTLineDraw ]
95 [ open-font line metrics dim ] dip 0 0 f
100 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
102 : ref/unref-line ( line n -- )
103 '[ _ + ] change-refs 0 >>age drop ;
105 : ref-line ( line -- ) 1 ref/unref-line ;
106 : unref-line ( line -- ) -1 ref/unref-line ;
110 : cached-line ( font string -- line )
111 cached-lines get [ <line> ] 2cache ;
113 CONSTANT: max-line-age 10
116 [ 1+ ] change-age age>> max-line-age >= ;
118 : age-line ( line -- ? )
119 #! Outputs t whether the line is dead.
120 dup refs>> 0 = [ age ] [ drop f ] if ;
122 : age-assoc ( assoc quot -- assoc' )
123 '[ nip @ ] assoc-partition
124 [ values dispose-each ] dip ; inline
127 cached-lines global [ [ age-line ] age-assoc ] change-at ;
129 [ H{ } clone cached-lines set-global ] "core-text" add-init-hook