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 font 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 :: fill-selection-background ( context loc dim line string -- )
91 context string color>> >rgba-components CGContextSetRGBFillColor
92 context dim line string selection-rect
93 dup CGRect-x loc first - over set-CGRect-x
97 : line-rect ( line -- rect )
98 dummy-context CTLineGetImageBounds ;
100 : set-text-position ( context loc -- )
101 first2 [ neg ] bi@ CGContextSetTextPosition ;
103 :: line-loc ( metrics loc dim -- loc )
105 metrics ascent>> ceiling dim second loc second + - 2array ;
107 :: <line> ( font string -- line )
109 [let* | open-font [ font cache-font CFRetain |CFRelease ]
110 line [ string open-font font foreground>> <CTLine> |CFRelease ]
112 rect [ line line-rect ]
113 (loc) [ rect CGRect-origin CGPoint>loc ]
114 (dim) [ rect CGRect-size CGSize>dim ]
115 (ext) [ (loc) (dim) v+ ]
116 loc [ (loc) [ floor ] map ]
117 ext [ (loc) (dim) [ + ceiling ] 2map ]
118 dim [ ext loc [ - >integer ] 2map ]
119 metrics [ open-font line compute-line-metrics ] |
120 open-font line metrics
123 [ font dim fill-background ]
124 [ loc dim line string fill-selection-background ]
125 [ loc set-text-position ]
126 [ [ line ] dip CTLineDraw ]
129 metrics loc dim line-loc
135 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
139 : cached-line ( font string -- line )
140 cached-lines get [ <line> ] 2cache ;
142 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook