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 io.encodings.string io.encodings.utf16 kernel make
8 math math.functions math.vectors namespaces opengl sequences
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 )
35 MEMO: make-attributes ( open-font color -- hashtable )
37 kCTForegroundColorAttributeName ,,
38 kCTFontAttributeName ,,
41 : <CTLine> ( string open-font color -- line )
44 dup selection? [ string>> ] when
47 make-attributes <CFAttributedString> &CFRelease
48 CTLineCreateWithAttributedString
51 TUPLE: line < disposable font string line metrics image loc dim
52 render-loc render-dim render-ext ;
54 : typographic-bounds ( line -- width ascent descent leading )
55 { CGFloat CGFloat CGFloat }
56 [ CTLineGetTypographicBounds ] with-out-parameters ; inline
58 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
66 : compute-font-metrics ( metrics font -- metrics )
67 [ CTFontGetCapHeight >>cap-height ]
68 [ CTFontGetXHeight >>x-height ]
71 : compute-line-metrics ( open-font line -- line-metrics )
73 [ compute-font-metrics ]
74 [ typographic-bounds store-typographic-bounds ] bi*
77 : metrics>dim ( bounds -- dim )
78 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
79 ceiling >integer 2array ;
81 : fill-background ( context font dim -- )
82 [ background>> >rgba-components CGContextSetRGBFillColor ]
83 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
86 : selection-rect ( dim line selection -- rect )
87 [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
88 start end [ 0 swap string subseq utf16n encode length 2 /i ] bi@
90 [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
91 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
93 : CGRect-translate-x ( CGRect x -- CGRect' )
94 [ dup CGRect-x ] dip - over set-CGRect-x ;
96 :: fill-selection-background ( context loc dim line string -- )
98 context string color>> >rgba-components CGContextSetRGBFillColor
99 context dim line string selection-rect
100 loc first CGRect-translate-x
104 : line-rect ( line -- rect )
105 dummy-context CTLineGetImageBounds ;
107 : set-text-position ( context loc -- )
108 first2 [ neg ] bi@ CGContextSetTextPosition ;
110 :: line-loc ( metrics loc dim -- loc )
112 metrics ascent>> dim second loc second + - 1 - 2array ;
115 gl-scale-factor get-global [ 1.0 > ] [ f ] if* ;
117 :: <line> ( font string -- line )
120 font load-2x? [ cache-font@2x ] [ cache-font ] if :> open-font
121 string open-font font foreground>> <CTLine> |CFRelease :> line
122 open-font line compute-line-metrics
123 [ >>metrics ] [ metrics>dim >>dim ] bi
129 :: render ( line -- line image )
130 line line>> :> ctline
131 line string>> :> string
136 ctline line-rect :> rect
137 rect origin>> CGPoint>loc :> (loc)
138 rect size>> CGSize>dim :> (dim)
141 (dim) vceiling :> dim
142 dim [ >integer 1 + ] map :> ext
144 loc line render-loc<<
145 dim line render-dim<<
146 ext line render-ext<<
148 line metrics>> loc dim line-loc line loc<<
152 line render-loc>> :> loc
153 line render-dim>> :> dim
154 line render-ext>> :> ext
158 [ font ext fill-background ]
159 [ loc first 0 2array dim first ext second 2array ctline string fill-selection-background ]
160 [ loc set-text-position ]
161 [ [ ctline ] dip CTLineDraw ]
163 ] make-bitmap-image load-2x? >>2x? ;
165 : line>image ( line -- image )
166 dup image>> [ render >>image ] unless image>> ;
168 M: line dispose* line>> CFRelease ;
172 : cached-line ( font string -- line )
173 cached-lines get-global [ <line> ] 2cache ;
175 STARTUP-HOOK: [ <cache-assoc> cached-lines set-global ]