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 destructors
4 accessors fry words hashtables strings sequences memoize assocs math
5 math.functions locals init namespaces combinators fonts colors cache
6 core-foundation core-foundation.strings core-foundation.attributed-strings
7 core-foundation.utilities core-graphics core-graphics.types
8 core-text.fonts core-text.utilities ;
11 TYPEDEF: void* CTLineRef
13 C-GLOBAL: kCTFontAttributeName
14 C-GLOBAL: kCTKernAttributeName
15 C-GLOBAL: kCTLigatureAttributeName
16 C-GLOBAL: kCTForegroundColorAttributeName
17 C-GLOBAL: kCTParagraphStyleAttributeName
18 C-GLOBAL: kCTUnderlineStyleAttributeName
19 C-GLOBAL: kCTVerticalFormsAttributeName
20 C-GLOBAL: kCTGlyphInfoAttributeName
22 FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
24 FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
26 FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
28 FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
30 FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
32 FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
34 ERROR: not-a-string object ;
36 : <CTLine> ( string open-font color -- line )
39 dup selection? [ string>> ] when
40 dup string? [ not-a-string ] unless
43 kCTForegroundColorAttributeName set
44 kCTFontAttributeName set
45 ] H{ } make-assoc <CFAttributedString> &CFRelease
46 CTLineCreateWithAttributedString
49 TUPLE: line font line metrics image disposed ;
51 : compute-line-metrics ( line -- line-metrics )
52 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
53 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
56 : bounds>dim ( bounds -- dim )
57 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
61 : fill-background ( context font dim -- )
62 [ background>> >rgba-components CGContextSetRGBFillColor ]
63 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
66 : selection-rect ( dim line selection -- rect )
67 [ start>> ] [ end>> ] bi
68 [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
69 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
71 :: fill-selection-background ( context dim line string -- )
73 context string color>> >rgba-components CGContextSetRGBFillColor
74 context dim line string selection-rect CGContextFillRect
77 : set-text-position ( context metrics -- )
78 [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
80 :: <line> ( font string -- line )
82 [let* | open-font [ font cache-font CFRetain |CFRelease ]
83 line [ string open-font font foreground>> <CTLine> |CFRelease ]
84 metrics [ line compute-line-metrics ]
85 dim [ metrics bounds>dim ] |
86 open-font line metrics
89 [ font dim fill-background ]
90 [ dim line string fill-selection-background ]
91 [ metrics set-text-position ]
92 [ [ line ] dip CTLineDraw ]
99 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
103 : cached-line ( font string -- line )
104 cached-lines get [ <line> ] 2cache ;
106 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook