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 ( open-font line -- line-metrics )
54 [ CTFontGetCapHeight >>cap-height ]
55 [ CTFontGetXHeight >>x-height ]
58 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
59 [ CTLineGetTypographicBounds ] 3keep
63 [ *CGFloat >>descent ]
64 [ *CGFloat >>leading ]
68 : bounds>dim ( bounds -- dim )
69 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
73 : fill-background ( context font dim -- )
74 [ background>> >rgba-components CGContextSetRGBFillColor ]
75 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
78 : selection-rect ( dim line selection -- rect )
79 [ start>> ] [ end>> ] bi
80 [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
81 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
83 :: fill-selection-background ( context dim line string -- )
85 context string color>> >rgba-components CGContextSetRGBFillColor
86 context dim line string selection-rect CGContextFillRect
89 : set-text-position ( context metrics -- )
90 [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
92 :: <line> ( font string -- line )
94 [let* | open-font [ font cache-font CFRetain |CFRelease ]
95 line [ string open-font font foreground>> <CTLine> |CFRelease ]
96 metrics [ open-font line compute-line-metrics ]
97 dim [ metrics bounds>dim ] |
98 open-font line metrics
101 [ font dim fill-background ]
102 [ dim line string fill-selection-background ]
103 [ metrics set-text-position ]
104 [ [ line ] dip CTLineDraw ]
111 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
115 : cached-line ( font string -- line )
116 cached-lines get [ <line> ] 2cache ;
118 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook