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
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 : <CTLine> ( string open-font color -- line )
38 kCTForegroundColorAttributeName set
39 kCTFontAttributeName set
40 ] H{ } make-assoc <CFAttributedString> &CFRelease
41 CTLineCreateWithAttributedString
44 TUPLE: line font line bounds dim bitmap age refs disposed ;
46 TUPLE: typographic-bounds width ascent descent leading ;
48 : line-typographic-bounds ( line -- typographic-bounds )
49 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
50 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
51 typographic-bounds boa ;
53 : bounds>dim ( bounds -- dim )
54 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
58 :: <line> ( string font -- line )
60 [let* | open-font [ font cache-font CFRetain |CFRelease ]
61 line [ string open-font font foreground>> <CTLine> |CFRelease ]
62 bounds [ line line-typographic-bounds ]
63 dim [ bounds bounds>dim ] |
66 [ font background>> >rgba-components CGContextSetRGBFillColor ]
67 [ 0 0 dim first2 <CGRect> CGContextFillRect ]
68 [ 0 bounds descent>> CGContextSetTextPosition ]
69 [ line swap CTLineDraw ]
72 [ open-font line bounds dim ] dip 0 0 f
77 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
79 : ref/unref-line ( line n -- )
80 '[ _ + ] change-refs 0 >>age drop ;
82 : ref-line ( line -- ) 1 ref/unref-line ;
83 : unref-line ( line -- ) -1 ref/unref-line ;
87 : cached-line ( string font -- line )
88 cached-lines get [ <line> ] 2cache ;
90 CONSTANT: max-line-age 10
93 [ 1+ ] change-age age>> max-line-age >= ;
95 : age-line ( line -- ? )
96 #! Outputs t whether the line is dead.
97 dup refs>> 0 = [ age ] [ drop f ] if ;
99 : age-assoc ( assoc quot -- assoc' )
100 '[ nip @ ] assoc-partition
101 [ values dispose-each ] dip ;
104 cached-lines global [ [ age-line ] age-assoc ] change-at ;
106 [ H{ } clone cached-lines set-global ] "core-text" add-init-hook