1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.syntax arrays
4 assocs cache classes colors combinators core-foundation
5 core-foundation.attributed-strings core-foundation.strings
6 core-graphics core-graphics.types core-text.fonts destructors
7 fonts init io.encodings.string io.encodings.utf16n kernel make
8 math math.functions math.order math.vectors namespaces
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 MEMO: make-attributes ( open-font color -- hashtable )
39 kCTForegroundColorAttributeName ,,
40 kCTFontAttributeName ,,
43 : <CTLine> ( string open-font color -- line )
46 dup selection? [ string>> ] when
49 make-attributes <CFAttributedString> &CFRelease
50 CTLineCreateWithAttributedString
53 TUPLE: line < disposable font string line metrics image loc dim
54 render-loc render-dim render-ext ;
56 : typographic-bounds ( line -- width ascent descent leading )
57 { CGFloat CGFloat CGFloat }
58 [ CTLineGetTypographicBounds ] with-out-parameters ; inline
60 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
68 : compute-font-metrics ( metrics font -- metrics )
69 [ CTFontGetCapHeight >>cap-height ]
70 [ CTFontGetXHeight >>x-height ]
73 : compute-line-metrics ( open-font line -- line-metrics )
75 [ compute-font-metrics ]
76 [ typographic-bounds store-typographic-bounds ] bi*
79 : metrics>dim ( bounds -- dim )
80 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
81 ceiling >integer 2array ;
83 : fill-background ( context font dim -- )
84 [ background>> >rgba-components CGContextSetRGBFillColor ]
85 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
88 : selection-rect ( dim line selection -- rect )
89 [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
90 start end [ 0 swap string subseq utf16n encode length 2 /i ] bi@
92 [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
93 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
95 : CGRect-translate-x ( CGRect x -- CGRect' )
96 [ dup CGRect-x ] dip - over set-CGRect-x ;
98 :: fill-selection-background ( context loc dim line string -- )
100 context string color>> >rgba-components CGContextSetRGBFillColor
101 context dim line string selection-rect
102 loc first CGRect-translate-x
106 : line-rect ( line -- rect )
107 dummy-context CTLineGetImageBounds ;
109 : set-text-position ( context loc -- )
110 first2 [ neg ] bi@ CGContextSetTextPosition ;
112 :: line-loc ( metrics loc dim -- loc )
114 metrics ascent>> dim second loc second + - 1 - 2array ;
116 :: <line> ( font string -- line )
119 font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font
120 string open-font font foreground>> <CTLine> |CFRelease :> line
121 open-font line compute-line-metrics
122 [ >>metrics ] [ metrics>dim >>dim ] bi
128 :: render ( line -- line image )
129 line line>> :> ctline
130 line string>> :> string
135 ctline line-rect :> rect
136 rect origin>> CGPoint>loc :> (loc)
137 rect size>> CGSize>dim :> (dim)
140 (dim) vceiling :> dim
141 dim [ >integer 1 + ] map :> ext
143 loc line render-loc<<
144 dim line render-dim<<
145 ext line render-ext<<
147 line metrics>> loc dim line-loc line loc<<
151 line render-loc>> :> loc
152 line render-dim>> :> dim
153 line render-ext>> :> ext
157 [ font ext fill-background ]
158 [ loc first 0 2array dim first ext second 2array ctline string fill-selection-background ]
159 [ loc set-text-position ]
160 [ [ ctline ] dip CTLineDraw ]
162 ] make-bitmap-image retina? get-global >>2x? ;
164 : line>image ( line -- image )
165 dup image>> [ render >>image ] unless image>> ;
167 M: line dispose* line>> CFRelease ;
171 : cached-line ( font string -- line )
172 cached-lines get-global [ <line> ] 2cache ;
174 [ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook