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.vectors math.rectangles math.functions locals init namespaces
6 combinators fonts colors cache core-foundation core-foundation.strings
7 core-foundation.attributed-strings core-foundation.utilities
8 core-graphics core-graphics.types 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 line metrics image loc dim disposed ;
51 : typographic-bounds ( line -- width ascent descent leading )
52 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
53 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ ; inline
55 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
63 : compute-font-metrics ( metrics font -- metrics )
64 [ CTFontGetCapHeight >>cap-height ]
65 [ CTFontGetXHeight >>x-height ]
68 : compute-line-metrics ( open-font line -- line-metrics )
70 [ compute-font-metrics ]
71 [ typographic-bounds store-typographic-bounds ] bi*
74 : metrics>dim ( bounds -- dim )
75 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
79 : fill-background ( context font dim -- )
80 [ background>> >rgba-components CGContextSetRGBFillColor ]
81 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
84 : selection-rect ( dim line selection -- rect )
85 [ start>> ] [ end>> ] bi
86 [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
87 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
89 : CGRect-translate-x ( CGRect x -- CGRect' )
90 [ dup CGRect-x ] dip - over set-CGRect-x ;
92 :: fill-selection-background ( context loc dim line string -- )
94 context string color>> >rgba-components CGContextSetRGBFillColor
95 context dim line string selection-rect
96 loc first CGRect-translate-x
100 : line-rect ( line -- rect )
101 dummy-context CTLineGetImageBounds ;
103 : set-text-position ( context loc -- )
104 first2 [ neg ] bi@ CGContextSetTextPosition ;
106 :: line-loc ( metrics loc dim -- loc )
108 metrics ascent>> ceiling dim second loc second + - 2array ;
110 :: <line> ( font string -- line )
112 [let* | open-font [ font cache-font ]
113 line [ string open-font font foreground>> <CTLine> |CFRelease ]
115 rect [ line line-rect ]
116 (loc) [ rect CGRect-origin CGPoint>loc ]
117 (dim) [ rect CGRect-size CGSize>dim ]
118 (ext) [ (loc) (dim) v+ ]
119 loc [ (loc) [ floor ] map ]
120 ext [ (loc) (dim) [ + ceiling ] 2map ]
121 dim [ ext loc [ - >integer ] 2map ]
122 metrics [ open-font line compute-line-metrics ] |
126 [ font dim fill-background ]
127 [ loc dim line string fill-selection-background ]
128 [ loc set-text-position ]
129 [ [ line ] dip CTLineDraw ]
132 metrics loc dim line-loc
138 M: line dispose* line>> CFRelease ;
142 : cached-line ( font string -- line )
143 cached-lines get [ <line> ] 2cache ;
145 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook